Is it possible to load QML libraries at runtime like Qt Plugins are loaded with QPluginLoader?
-
Hello,
I need to develop an application that supports machines of different models and from different vendors.
Models have different capabilities, so they require different interfaces (some pieces are common, some aren't etc.).
Nonetheless, each model has its own SDK, not even a vendor-wide one :(A coworker suggested to implement an application with Qt Plugins that will be loaded at runtime, allowing to plug in new models without modifying the main application.
He suggested to:- Create the main window that will act as a container
- Load all the plugins that I can find
- Each plugin will implement an interface that gives me the model it supports
- The plugin will implement the logic to communicate with the machine, and also provide the UI
- The main window will provide a container where the plugin can show the UI
My coworker also suggested to load the plugins via
QPluginLoader.
If I useqt_add_pluginto create a Qt Plugin with a class that inherits from QObject and an interface I defined, it works.
If I use a QML Module instead, it doesn't.
From the docs I've seen that a QML plugin shall inherit fromQQmlEngineExtensionPlugininstead of QObject.The code to load a plugin and use it with my interface is very simple:
QPluginLoader loader("Path/To/Plugin/MyPlugin.dll"); // This is never nullptr QObject* pluginInstance = loader.instance(); if(pluginInstance) { // This fails if I use a QML Module Interface* in = qobject_cast<Interface*>(pluginInstance); } -
Yes. You can load. Just call registerTypes() once you load through QPluginLoader.
-
Yes. You can load. Just call registerTypes() once you load through QPluginLoader.
@dheerendra thanks for the reply!
But how do I cast the instance to the plugin object?I mean if I have a QML Plugin class like this:
class QExampleQmlPlugin : public QQmlEngineExtensionPlugin { Q_OBJECT Q_PLUGIN_METADATA(IID QQmlEngineExtensionInterface_iid) public: void registerTypes(); };How do I call registerTypes?
As I've said, I couldn't cast it to the interface it has implemented...