mercredi 27 juillet 2016

Declare a plugin class that inherits from QWidget

The How to Create Qt Plugins says Qt application can be extended through plugins. Writing a plugin involves these steps:

  1. Declare a plugin class that inherits from QObject and from the interfaces that the plugin wants to provide.
  2. Use the Q_INTERFACES() macro to tell Qt's meta-object system about the interfaces.
  3. Export the plugin using the Q_PLUGIN_METADATA() macro.
  4. Build the plugin using a suitable .pro file.

Item 1. translates to something like:

class MyPlugin : public QObject, PluginInterface {...} 

Afterwards I'll load it like:

QPluginLoader pluginLoader( pluginsDir.absoluteFilePath(myPLugin) );
PluginInterface *p = pluginLoader.instance();

I'm interested in making a QWidget from a plugin. One way to do that is to add a method to the plugin that returns a QWidget, like:

virtual QWidget *panel() = 0;

It seems to work but it looks strange to me. Instead of this approach, how could I make a QWidget (or a class derived from QWidget) from a plugin?

Should I declare a plugin class that inherits from QWidget?

Aucun commentaire:

Enregistrer un commentaire