QWidget in a plugin [Solved]
-
wrote on 18 May 2011, 08:32 last edited by
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.
-
wrote on 18 May 2011, 08:40 last edited by
Have a look at the designer plugins, they return widgets among other things from the plugin.
-
wrote on 18 May 2011, 12:09 last edited by
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. -
wrote on 18 May 2011, 18:26 last edited by
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). -
wrote on 19 May 2011, 06:57 last edited by
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()); } } }
}
@ -
wrote on 19 May 2011, 06:59 last edited by
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....
-
wrote on 19 May 2011, 07:11 last edited by
where did you put the plugin (physically on the disk)?
-
wrote on 19 May 2011, 07:15 last edited by
As you can see in the debug output, it find the plugin but it can't load it.
-
wrote on 19 May 2011, 07:54 last edited by
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...
9/9