2

I have two qml Plugins and I would like to only import the plugin of choice at runtime or when starting the application such that I can use a single variable to represent either of the plugins. For example, I would like something similar to using C++ MACRO but in qml like:

#ifdef WHICH_PLUGIN
    import generic_plugin_A 1.0 as myPlugin
#else 
    import generic_plugin_B 1.0 as myPlugin
#endif

...
// then I can use myPlugin for calls/signals/... as long as both generic plugins have the same UI interface
...

The way I can get it to work is create a third plugin generic_plugin_C that I import in qml and then interface this plugin from the UI to either generic_plugin_A/B. I'm just wondering if there is a different or cleaner way of doing this that would be better.

A different approach I am trying is to use the qmlResisterType to implement this in main.cpp:

main.cpp:

...
if (WHICH_PLUGIN)
    qmlRegisterType<PluginA_ClassName>("genericplugin", 1, 0, "PluginA_ClassName");
else
    qmlRegisterType<PluginB_ClassName>("genericplugin", 1, 0, "PluginB_ClassName");
...

Then in my qml file:

import genericplugin 1.0

...

genricplugin.funcCall()
...

I expect to use the same variable name and signal/qproperty/invokable/etc in qml for which ever plugin is selected at the time.

slayer
  • 643
  • 7
  • 14
  • 2
    It seems to me somebody tried to solve exact type of this problem but had a difficulty with missing declaration of Q_OBJECT: https://stackoverflow.com/questions/20693957/expose-abstract-type-as-q-property-to-qml You may also expose all the properties and invokables with empty virtual functions for implementation from that abstract class. – Alexander V Jun 14 '19 at 22:35
  • 1
    Yes, this worked for the abstract class. – slayer Jun 20 '19 at 16:28

1 Answers1

2

It seems to me that the way to go is conditionally loading qml files via Loader. Those files can each have their own different imports.

See this answer for details: https://stackoverflow.com/a/52434062/1423877.

MarPan
  • 113
  • 1
  • 9
  • 1
    Thanks for the link. This is close to what I would like to do. I tried to implement a FileSelector with my own selector condition that would load plugins. It did not work but I think because the selector was loading the .dll of the plugin or maybe the selector doesn't work for plugins in general. – slayer Jun 20 '19 at 16:27