I want to make an application with plugins that also can have plugins (which the main application do not need to know about). Reason is that I want to make a general IDE for some code projects, then I make a image handler plugin. I would then like the image handler plugin to have some filter plugins of its own that the main application do not need to know about. Is this possible?
Here is what I got my self:
I would like to make a main IDE that have some interfaces.
class IDocumentFactory
{
public:
virtual ~IDocumentFactory() {}
//virtual QStringList documents() const = 0;
virtual QVector<FileOpenEntries> name_filters() const = 0;
virtual QWidget* open_document( QWidget* parent,
const QStringList &filepaths,
const QString &name_filter_key) const = 0;
};
Q_DECLARE_INTERFACE(IDocumentFactory, "S-DAIDE.IDocumentFactory/1.0")
class IDeclarePlugins
{
public:
virtual ~IDeclarePlugins() {}
virtual QStringList plugins() const = 0;
virtual void load_plugings(QObject * plugins) = 0;
};
Q_DECLARE_INTERFACE(IDeclarePlugins, "S-DAIDE.IDeclarePlugins/1.0")
Now I would like to define an interface in my plugin that other plugins can implement.
class MyPlugin : public QObject, IDeclarePlugins
{
Q_OBJECT
Q_INTERFACES(IDeclarePlugins)
...
QStringList ImagePlugin::plugins() const
{
return QStringList() << "S-DAIDE.ImagePlugin.IOverlay/1.0";
}
}
In my main app loading plugins.
foreach (QString fileName, m_plugins_dir.entryList(QDir::Files)) {
QPluginLoader loader(m_plugins_dir.absoluteFilePath(fileName));
QObject *plugin = loader.instance();
}
Do there exist a way to check if a plugin implements an interface based on the string used with Q_DECLARE_INTERFACE. Such I can make the plugin load all other plugins that it declare by the virtual QStringList plugins() const = 0;
By design, you should
qobject_castto interface classes, but not to use thoseids.You can still create a function
QString id()that will return youridfor every plugin, you can check that function result in your loader code.You can check, if your plugin has given
id, as you want, in principle, you can find the way to do it inQ_DECLARE_INTERFACEmacro defenition.But
qt_metacastis not documented, so I do not recommend you to use this method.If you want get rid of repeatedly writing ids 2 times, you can use macros, that Qt’s framework so love itself:
Study the output and note, that you should use
id()function instead ofiid_private