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 in a plugin [Solved]
Forum Updated to NodeBB v4.3 + New Features

QWidget in a plugin [Solved]

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 5.2k 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.
  • L Offline
    L Offline
    luca
    wrote on 18 May 2011, 08:32 last edited by
    #1

    Hi all,
    I'm playing for the first time with plugins and I'm doing some tests.

    I'm trying to create a very simple QMainWindow that load a plugin at run-time and put it as its central widget.

    All the plugins example I saw use plugins to add some function to the application, not a GUI (for example "this":http://doc.qt.nokia.com/4.7/tools-plugandpaint.html)

    I need a very small example on how to define interface to contain a QWidget.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on 18 May 2011, 08:40 last edited by
      #2

      Have a look at the designer plugins, they return widgets among other things from the plugin.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 18 May 2011, 12:09 last edited by
        #3

        Plugins supplying widgets is really not all that magical.
        While you can simply return a QWidget*, you might need to think about an interface that is a bit more specific to your application. In my application, I am using a Component class that functions as this base interface, and that can be extended with a set of other interfaces that may or may not be implemented by the plugin.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          luca
          wrote on 18 May 2011, 18:26 last edited by
          #4

          Thanks,
          I thought to the plugin because I need a way to update some widget of the application without the need to re-build all application.
          The application should take the name of the plugins from a DB and than lists all them in a menu (if it find some plugins).

          1 Reply Last reply
          0
          • L Offline
            L Offline
            luca
            wrote on 19 May 2011, 06:57 last edited by
            #5

            I have a problem in using the plugin:

            This is the interface:
            @
            #ifndef PLUGININTERFACE_H
            #define PLUGININTERFACE_H

            #include <QtPlugin>

            class QWidget;

            class PluginInterface
            {
            public:
            virtual ~PluginInterface() {}
            virtual QWidget* createWidget() = 0;
            };

            Q_DECLARE_INTERFACE(PluginInterface, "com.trolltech.Prova.PluginInterface/1.0")

            #endif // PLUGININTERFACE_H

            @

            this is the plugin:
            @
            #ifndef PLUGIN_H
            #define PLUGIN_H

            #include <QObject>
            #include <QWidget>
            #include "plugininterface.h"

            #include "widgetplugin.h"

            class Plugin : public QObject, public PluginInterface
            {
            Q_OBJECT
            Q_INTERFACES(PluginInterface)
            public:
            explicit Plugin(QObject *parent = 0);
            QWidget *createWidget();

            signals:

            public slots:

            };

            #endif // PLUGIN_H

            @

            @
            #include "plugin.h"

            Plugin::Plugin(QObject *parent) :
            QObject(parent)
            {
            }

            QWidget * Plugin::createWidget()
            {
            return new WidgetPlugin();
            }

            @

            the WidgetPlugin is a very simple plugin.

            This is the MainWindow:
            @
            #include "plugininterface.h"

            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
            {
            ui->setupUi(this);

            QDir pluginsDir = QDir(qApp->applicationDirPath());
            
            pluginsDir.cd("plugins");
            
            foreach (QString fileName, pluginsDir.entryList(QDir::Files))
            {
                qDebug() << fileName;
                qDebug() << pluginsDir.absoluteFilePath(fileName);
                QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
                //qDebug() << loader.loadHints();
                QObject *plugin = loader.instance();
                qDebug() << plugin;
                if (plugin)
                {
                    PluginInterface *plugin_widget = qobject_cast<PluginInterface *>(plugin);
                    if (plugin_widget)
                    {
                        this->setCentralWidget(plugin_widget->createWidget());
                    }
                }
            }
            

            }
            @

            1 Reply Last reply
            0
            • L Offline
              L Offline
              luca
              wrote on 19 May 2011, 06:59 last edited by
              #6

              After compiling the plugin I get a .so file:
              @
              libwidgetplugin.so
              @

              When I execute the MainWindow application I get in the debug:
              @
              "libwidgetplugin.so"
              "/home/luca/mainwindow-build-desktop/plugins/libwidgetplugin.so"
              QObject(0x0)
              @

              So it seems it isn't able to load plugin....

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on 19 May 2011, 07:11 last edited by
                #7

                where did you put the plugin (physically on the disk)?

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  luca
                  wrote on 19 May 2011, 07:15 last edited by
                  #8

                  As you can see in the debug output, it find the plugin but it can't load it.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    luca
                    wrote on 19 May 2011, 07:54 last edited by
                    #9

                    I changed plugin.cpp :
                    @
                    #include "plugin.h"

                    QWidget * Plugin::createWidget()
                    {
                    return new WidgetPlugin();
                    }

                    Q_EXPORT_PLUGIN2(widgetplugin, Plugin)

                    @

                    I solved the problem, now it works...

                    1 Reply Last reply
                    0

                    1/9

                    18 May 2011, 08:32

                    • Login

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