PDA

View Full Version : Dynamic widget plugin



bunjee
30th April 2009, 15:30
Hey there,

I'd like to:
- Load dynamically a low level Qt Plugin.
- That plugin contains some unique widgets.
- Then from my main application, I want to create and interact with that custom widget.

Is it possible or do I have to create an abstract interface for all my custom widgets?

Thanks.

faldzip
30th April 2009, 18:26
I think that your plugin can be abstract factory, so the interface can look like this:


class Interface
{
public:
QStringList availableWidgets() const = 0;
QWidget * createWidgetForName(const QString & widgetName, QWidget * parent = 0) const = 0;
};

// in plugin:
class Plugin : public Interface
{
/* ... */
};

QWidget * Plugin::createWidgetForName(const QString & widgetName) const
{
if (name == "WidgetOne")
return new WidgetOne(parent);
if (name == "WidgetTwo")
return new WidgetTwo(parent);
}
In such situation you can only declare that interface and made as many widgets as you want in you plugin shared library. The problem is that all the widgets you get as QWidget so you dont know anything about custom methods etc. So you have to include custom widgets headers in you main application and qobject_cast them.
At least that is what I would do :D when I face your problem.

P.S. Ohh, of course you can also use Property System and interact with widgets with property() and setProperty()