PDA

View Full Version : Loading QWidgets as a Plugin



aylek
25th May 2010, 21:25
I'm working on a plugin interface, which will be used to load a set of QWidgets into a host application. Currently, the interface provides a getWidget() method, which returns an instance to the widget. This method sounds obvious enough, but the problem lies in how the host application is handling the widgets. The host application only knows how to manage QWidget instances and not instances to a plugin's interface, thus losing access to other methods in the interface.

I have been able to solve this by having the interface class inherit QWidget. Subclassing QWidget ensures the interface methods are retained by the host application's widget manager.

See examples below...

First method:
class MyInterface1 {
public:
virtual void someMethod() = 0;
virtual QWidget* getWidget() = 0;
};

class MyPlugin1 : public QWidget, public MyInterface1 {
public:
void someMethod();
QWidget* getWidget();
};

Second method:
class MyInterface2 : public QWidget {
public:
virtual void someMethod() = 0;
};

class MyPlugin2 : public MyInterface2 {
public:
void someMethod();
};

My problem with this is that by subclassing QWidget, my interface class has wandered beyond the scope of being a pure plugin interface. Anyone else think there is a better approach?

Apologies if I may have confused anyone :D

SixDegrees
25th May 2010, 21:57
I'm not entirely sure what you're trying to do here, but the canonical way to have a plugin that also supports signals and slots or other QWidget-y properties is to create a wrapper class that is pure virtual, and contains a single function that returns an instance of the class/widget you're interested in. If you return a pointer to a base class, you can have a full-blown interface and inheritance hierarchy returned, and provide simple wrappers for all of them. The returned class is just a class - not a plugin. It is simply made accessible through the wrapper, which is a plugin.

aylek
25th May 2010, 23:34
I'm not entirely sure what you're trying to do here, but the canonical way to have a plugin that also supports signals and slots or other QWidget-y properties is to create a wrapper class that is pure virtual, and contains a single function that returns an instance of the class/widget you're interested in. If you return a pointer to a base class, you can have a full-blown interface and inheritance hierarchy returned, and provide simple wrappers for all of them. The returned class is just a class - not a plugin. It is simply made accessible through the wrapper, which is a plugin.

Thanks, I wasnt sure if casting an instance of a QWidget (using qobject_cast<>) would work, since normally one would cast the instance returned by QPluginLoader::instance(). I just tested it, and sure enough, casting the QWidget instance exposed the complete plugin interface. Thanks for your help!