Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qwidget plugin
Forum Updated to NodeBB v4.3 + New Features

Qwidget plugin

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 4 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H hjohn

    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.

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by raven-worx
    #3

    @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.

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    H 1 Reply Last reply
    1
    • J.HilkJ J.Hilk

      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.

      H Offline
      H Offline
      hjohn
      wrote on last edited by
      #4

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

      J.HilkJ 1 Reply Last reply
      0
      • H hjohn

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

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #5

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


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        H 1 Reply Last reply
        0
        • J.HilkJ J.Hilk

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

          H Offline
          H Offline
          hjohn
          wrote on last edited by
          #6

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

          1 Reply Last reply
          0
          • raven-worxR raven-worx

            @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.

            H Offline
            H Offline
            hjohn
            wrote on last edited by
            #7

            @raven-worx I don't think so

            jsulmJ raven-worxR 2 Replies Last reply
            0
            • H hjohn

              @raven-worx I don't think so

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #8

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

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              H 1 Reply Last reply
              0
              • jsulmJ jsulm

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

                H Offline
                H Offline
                hjohn
                wrote on last edited by
                #9

                @jsulm it makes no difference.

                jsulmJ 1 Reply Last reply
                0
                • H hjohn

                  @jsulm it makes no difference.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @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?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  H 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @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?

                    H Offline
                    H Offline
                    hjohn
                    wrote on last edited by
                    #11

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

                    jsulmJ 1 Reply Last reply
                    0
                    • H hjohn

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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

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

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      H 1 Reply Last reply
                      0
                      • H hjohn

                        @raven-worx I don't think so

                        raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by
                        #13

                        @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...

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

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

                          H Offline
                          H Offline
                          hjohn
                          wrote on last edited by
                          #14

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

                          jsulmJ 1 Reply Last reply
                          0
                          • H hjohn

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

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            @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?

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            H 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @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?

                              H Offline
                              H Offline
                              hjohn
                              wrote on last edited by hjohn
                              #16

                              @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);
                                  }
                              }
                              
                              
                              jsulmJ 1 Reply Last reply
                              0
                              • H hjohn

                                @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);
                                    }
                                }
                                
                                
                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #17

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

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                H 1 Reply Last reply
                                0
                                • jsulmJ jsulm

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

                                  H Offline
                                  H Offline
                                  hjohn
                                  wrote on last edited by hjohn
                                  #18

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

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • H hjohn

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

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #19

                                    @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.

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    H 1 Reply Last reply
                                    0
                                    • jsulmJ jsulm

                                      @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.

                                      H Offline
                                      H Offline
                                      hjohn
                                      wrote on last edited by
                                      #20

                                      @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.

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • H hjohn

                                        @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.

                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #21

                                        @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.

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        H 1 Reply Last reply
                                        2
                                        • jsulmJ jsulm

                                          @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.

                                          H Offline
                                          H Offline
                                          hjohn
                                          wrote on last edited by
                                          #22

                                          @jsulm thanks..:)

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved