Qwidget plugin



  • widgetplugin.cpp

    #include "widgetplugins.h"
    #include<QtPlugin>
    
    
    void widgetPlugins::initialize(QDesignerFormEditorInterface *core)
    {
        if (initialized)
            return;
    
        initialized = true;
    }
    widgetPlugins::widgetPlugins(QObject *parent)
        : QObject(parent)
    {
    }
    
    bool widgetPlugins::isInitialized() const
    {
        return initialized;
    }
    
    QWidget *widgetPlugins::createWidget(QWidget *parent)
    {
        return new MainWindow(parent);
    }
    
    QString widgetPlugins::name() const
    {
        return QStringLiteral("AnalogClock");
    }
    
    QString widgetPlugins::group() const
    {
        return QStringLiteral("Display Widgets [Examples]");
    }
    
    QIcon widgetPlugins::icon() const
    {
        return QIcon();
    }
    
    QString widgetPlugins::toolTip() const
    {
        return QString();
    }
    
    QString widgetPlugins::whatsThis() const
    {
        return QString();
    }
    
    bool widgetPlugins::isContainer() const
    {
        return false;
    }
    
    QString widgetPlugins::domXml() const
    {
        return "<ui language=\"c++\">\n"
               " <widget class=\"AnalogClock\" name=\"analogClock\">\n"
               "  <property name=\"geometry\">\n"
               "   <rect>\n"
               "    <x>0</x>\n"
               "    <y>0</y>\n"
               "    <width>100</width>\n"
               "    <height>100</height>\n"
               "   </rect>\n"
               "  </property>\n"
               "  <property name=\"toolTip\" >\n"
               "   <string>The current time</string>\n"
               "  </property>\n"
               "  <property name=\"whatsThis\" >\n"
               "   <string>The analog clock widget displays the current time.</string>\n"
               "  </property>\n"
               " </widget>\n"
               "</ui>\n";
    }
    
    QString widgetPlugins::includeFile() const
    {
        return QStringLiteral("mainwindow.h");
    }
    

    widgetplugin.h

    #ifndef WIDGETPLUGINS_H
    #define WIDGETPLUGINS_H
    #include<QtUiPlugin/QDesignerCustomWidgetInterface>
    #include<QObject>
    #include<QString>
    #include<QIcon>
    #include<QWidget>
    #include"mainwindow.h"
    class MainWindow;
    class widgetPlugins:public QObject,QDesignerCustomWidgetInterface
    {
        Q_OBJECT
        Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface")
        Q_INTERFACES(QDesignerCustomWidgetInterface)
    public:
        explicit widgetPlugins(QObject *parent = nullptr);
          bool isContainer() const override;
           bool isInitialized() const override;
           QIcon icon() const override;
           QString domXml() const override;
           QString group() const override;
           QString includeFile() const override;
           QString name() const override;
           QString toolTip() const override;
           QString whatsThis() const override;
           QWidget *createWidget(QWidget *parent) override;
           void initialize(QDesignerFormEditorInterface *core) override;
    
       private:
           bool initialized = false;
    
    };
    
    #endif // WIDGETPLUGINS_H
    

    .pro file

    CONFIG      += plugin
    TEMPLATE    = lib
    QT          += widgets uiplugin
    INCLUDEPATH    +="C:\Users\Kits-User1\Documents\QT_projects\custmW\widget"
    
    TARGET = $$qtLibraryTarget($$TARGET)
    
    target.path = $$[QT_INSTALL_PLUGINS]C:\Users\Kits-User1\Documents\QT_projects\custmW\designer
    INSTALLS += target
    
    
    
    HEADERS += \
        widgetplugins.h
    
    SOURCES += \
        widgetplugins.cpp
    
    DISTFILES += \
        Clock.json
    
    

    i am getting error of :

    : error: undefined reference to `MainWindow::MainWindow(QWidget*)'
    

    in QWidget *widgetPlugins::createWidget(QWidget *parent) function.



  • hi @hjohn ,

    I would suggest removing
    in Qwidget plugin:

    #include"mainwindow.h"
    class MainWindow;

    from your class.
    In your class there's no MainWindow object or class pointer created, so its uneeded and apparently the source of your error.


  • Moderators

    @hjohn said in Qwidget plugin:

    : error: undefined reference to `MainWindow::MainWindow(QWidget*)'

    means that the linker can't find the implementation of MainWindow.

    Did you add the cpp file to the project, so it gets compiled along with your plugin?
    Nevermind, you've posted your pro file. In there the files for your MainWindow class are missing.



  • @J.Hilk i have tried to remove class mainwindw;
    but it still give same error



  • @hjohn
    removing #include"mainwindow.h" is actually more important



  • @J.Hilk then how can i create mainwindow object in that class.I think it will give error of undeclare.



  • @raven-worx I don't think so


  • Moderators

    @hjohn Include #include"mainwindow.h" in your cpp file not header file.



  • @jsulm it makes no difference.


  • Moderators

    @hjohn That's because your pro file is missing mainwindow.*

    HEADERS += \
        widgetplugins.h
        ?
    
    SOURCES += \
        widgetplugins.cpp
        ?
    

    But onw question: is this main window part of your plugin or main application?



  • @jsulm it is partof main application.I want to use it in plugin.


  • Moderators

    @hjohn Why do you want to access the main window directly from the plug-in? It would be better to communicate via signals/slots.


  • Moderators

    @hjohn said in Qwidget plugin:

    @raven-worx I don't think so

    you don't think what?!
    If you want to use it you have to tell the compiler/linker tell where to find it...



  • @jsulm I am implementing qwidgetplugin with reference of http://doc.qt.io/qt-5/qtdesigner-customwidgetplugin-example.html


  • Moderators

    @hjohn You are aware that this link describes how to implement a plug-in for QtDesigner? Do you want a plug-in for QtDesigner or for your application? And in that example I can't see the plug-in using the main window. So again: why do you want to access main window from your plug-in directly?



  • @jsulm yeah i want plug-in for Qt designer.i have implemented same way as mentioned. Here is aq misunderstanding of "mainwindow" name. I have just give different name to the class.the definition and characteristics are same.

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include<QWidget>
    #include<QObject>
    #include<QtUiPlugin/qdesignerexportwidget>
    class MainWindow : public QWidget
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0);
    protected:
        void paintEvent(QPaintEvent *event) override;
    
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include <QMouseEvent>
    #include <QPainter>
    #include <QTime>
    #include <QTimer>
    
    MainWindow::MainWindow(QWidget *parent)
        : QWidget(parent)
    {
        QTimer *timer = new QTimer(this);
        connect(timer, &QTimer::timeout, this, QOverload<>::of(&QWidget::update));
        timer->start(1000);
    
        setWindowTitle(tr("Analog Clock"));
        resize(200, 200);
    }
    
    
    
    
    void MainWindow::paintEvent(QPaintEvent *)
    {
        static const QPoint hourHand[3] = {
            QPoint(7, 8),
            QPoint(-7, 8),
            QPoint(0, -40)
        };
        static const QPoint minuteHand[3] = {
            QPoint(7, 8),
            QPoint(-7, 8),
            QPoint(0, -70)
        };
    
        QColor hourColor(127, 0, 127);
        QColor minuteColor(0, 127, 127, 191);
    
        int side = qMin(width(), height());
        QTime time = QTime::currentTime();
    
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.translate(width() / 2, height() / 2);
        painter.scale(side / 200.0, side / 200.0);
    
        painter.setPen(Qt::NoPen);
        painter.setBrush(hourColor);
    
        painter.save();
        painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
        painter.drawConvexPolygon(hourHand, 3);
        painter.restore();
    
        painter.setPen(hourColor);
    
        for (int i = 0; i < 12; ++i) {
            painter.drawLine(88, 0, 96, 0);
            painter.rotate(30.0);
        }
    
        painter.setPen(Qt::NoPen);
        painter.setBrush(minuteColor);
    
        painter.save();
        painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
        painter.drawConvexPolygon(minuteHand, 3);
        painter.restore();
    
        painter.setPen(minuteColor);
    
        for (int j = 0; j < 60; ++j) {
            if ((j % 5) != 0)
                painter.drawLine(92, 0, 96, 0);
            painter.rotate(6.0);
        }
    }
    
    

  • Moderators

    @hjohn Then please add your mainwindow.h|cpp to the pro file as @raven-worx already suggested.



  • @jsulm okay thanks.. i will try as u have guided.
    one last question how Qdesigner is differ from normal gui application.


  • Moderators

    @hjohn It is simply a Qt application. But it provides a plug-in interface to add functionality to it. I mean functionality specific for QtDesigner.



  • @jsulm Qtdesigner does not contain any .ui file.i mean it is custom implementation .am i right?

    In other plugins example the project having subproject.for ex: gui application is the parent project and plugin lib contain in sub-project.Can we use same method in this qtdesigner plugin.


  • Moderators

    @hjohn I'm not sure I understand the question.
    QtDesigner is a Qt application. You can download its source code if you need it.
    The examples simply combine the main app and the plug-in in one project. In your case you can't do this as you would need to add your plug-in to QtDesigner source code. But it is not necessary, just follow the example you posted before.



  • @jsulm thanks..:)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.