Russian Qt Forum
Май 15, 2024, 16:33 *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

Войти
 
  Начало   Форум  WIKI (Вики)FAQ Помощь Поиск Войти Регистрация  

Страниц: [1]   Вниз
  Печать  
Автор Тема: Ошибка при использовании Пользовательского Виджета  (Прочитано 3498 раз)
NetWorm
Гость
« : Август 02, 2011, 18:21 »

Вобщем ситуация следующая!
Я взял простейший пример по созданию пользовательского виджета.
qclearlineedit.h
Код:
#ifndef FILTEREDIT_H
#define FILTEREDIT_H

#include <QVBoxLayout>
#include <QToolButton>
#include <QLineEdit>

class QClearLineEdit : public QLineEdit
{
Q_OBJECT
public:
    explicit QClearLineEdit(QWidget *parent = 0);
private:
    QToolButton *button;
private slots:
    void textChanged(QString);
};

#endif // FILTEREDIT_H
qclearlineedit.cpp
Код:
#include "qclearlineedit.h"

QClearLineEdit::QClearLineEdit(QWidget *parent) :
    QLineEdit(parent)
{
    button=new QToolButton(this);
    button->setCursor(Qt::ArrowCursor);
    button->setGeometry(0,0,15,15);
    button->setText("...");
    button->setFocusPolicy(Qt::NoFocus);
    button->setIcon(QIcon::fromTheme("edit-clear"));
    button->show();
    button->setStyleSheet("border: none;");
    connect(this,SIGNAL(textChanged(QString)),this,SLOT(textChanged(QString)));
    connect(button,SIGNAL(clicked()),this,SLOT(clear()));

    QVBoxLayout *layout=new QVBoxLayout(this);
    layout->addWidget(button,0,Qt::AlignRight);
    layout->setSpacing(0);
    layout->setMargin(0);
}

void QClearLineEdit::textChanged(QString text)
{
    if (text.isEmpty()) button->hide();
        else button->show();
}

далее код плагина
qclearlineeditplugin.h
Код:
#ifndef QCLEARLINEEDITPLUGIN_H
#define QCLEARLINEEDITPLUGIN_H

#include <QDesignerCustomWidgetInterface>
class QClearLineEditPlugin : public QObject, public QDesignerCustomWidgetInterface
{
    Q_OBJECT
    Q_INTERFACES(QDesignerCustomWidgetInterface)
public:
    QClearLineEditPlugin(QObject *parent = 0);
    bool isContainer() const;
    bool isInitialized() const;
    QIcon icon() const;
    QString domXml() const;
    QString group() const;
    QString includeFile() const;
    QString name() const;
    QString toolTip() const;
    QString whatsThis() const;
    QWidget *createWidget(QWidget *parent);
    void initialize(QDesignerFormEditorInterface *core);
private:
    bool m_initialized;

};
#endif

qclearlineeditplugin.cpp

Код:
#include "qclearlineedit.h"
#include "qclearlineeditplugin.h"
#include <QtCore/QtPlugin>

QClearLineEditPlugin::QClearLineEditPlugin(QObject *parent)

    : QObject(parent)

{
    m_initialized = false;
}

void QClearLineEditPlugin::initialize(QDesignerFormEditorInterface * /* core */)

{

    if (m_initialized)

        return;
    // Add extension registrations, etc. here
    m_initialized = true;

}

bool QClearLineEditPlugin::isInitialized() const

{
    return m_initialized;
}

QWidget *QClearLineEditPlugin::createWidget(QWidget *parent)

{
    return new QClearLineEdit(parent);
}

QString QClearLineEditPlugin::name() const

{
    return QLatin1String("QClearLineEdit");
}

QString QClearLineEditPlugin::group() const

{
    return QLatin1String("");
}

QIcon QClearLineEditPlugin::icon() const

{
    return QIcon();
}

QString QClearLineEditPlugin::toolTip() const

{
    return QLatin1String("");
}

QString QClearLineEditPlugin::whatsThis() const

{
    return QLatin1String("");
}

bool QClearLineEditPlugin::isContainer() const
{
    return false;
}

QString QClearLineEditPlugin::domXml() const
{
    return QLatin1String("<widget class=\"QClearLineEdit\" name=\"qClearLineEdit\">\n</widget>\n");
}

QString QClearLineEditPlugin::includeFile() const
{
    return QLatin1String("qclearlineedit.h");

}
Q_EXPORT_PLUGIN2(qclearlineeditplugin, QClearLineEditPlugin)


В результате создается файл qclearlineeditplugin.dll его я копирую в папку C:\Qt\4.7.3\plugins\designer
файлы qclearlineedit.h qclearlineedit.cpp в папку C:\Qt\4.7.3\include\QtGui...

Далее запуская Creator виджет виден. его можно поместить на форму, но вот при компиляции выдает слудющее:
release/mainwindow.o:mainwindow.cpp:(.text$_ZN13Ui_MainWindow7setupUiEP11QMainWindow[Ui_MainWindow::setupUi(QMainWindow*)]+0x129): undefined reference to `QClearLineEdit::QClearLineEdit(QWidget*)'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [release/untitled.exe] Error 1
mingw32-make: *** [release] Error 2

У кого какие есть идеи?Непонимающий
Записан
NetWorm
Гость
« Ответ #1 : Август 02, 2011, 21:03 »

Так вроде с чем то разобрался немного. нужно просто подключить эти два файла к проекту.

Код:
class QEditButton : public QToolButton
{
    Q_OBJECT
public:
    explicit QEditButton(QWidget *parent = 0);
public slots:
    void clicked();

Q_SIGNALS:
    void clicked();
};


class QClearLineEdit : public QLineEdit
{
    Q_OBJECT
public:
    explicit QClearLineEdit(QWidget *parent = 0);
private:
    QEditButton *button;
private slots:
    void textChanged(QString);

};


КАк мне сделать так чтобы Q_SIGNALS: void clicked(); относилось к кнопке и было видно из Креатора. т.е. я мог просто привязаться к событию по кнопке...

Вообще я делаю LineEdit с кнопкой по нажатию на которую мне нужно чтоб открылось диалоговое окно и можно было что-то выбрать.... Цель же сейчас одна привязаться сделать сигнал Q_SIGNALS: void clicked(); кнопки видимым из вне и в креаторе
Записан
NetWorm
Гость
« Ответ #2 : Август 03, 2011, 09:10 »

Кажись разобрался Сам!
Записан
Страниц: [1]   Вверх
  Печать  
 
Перейти в:  


Страница сгенерирована за 0.043 секунд. Запросов: 17.