Important: Please read the Qt Code of Conduct -

QPluginLoader : How to load several times the same plugin

  • Hello,

    I have developped several plugins I load inside an GUI application.
    The first time I load the plugin, all the stuff is working fine, but I didn't manage to load the same plugin another time.

    Each plugin is a widget that can be shown one or more times with different parameters.

    I apologize for the lack of code, but it is quite a basic code to load a plugin (get a plugin instance, and cast it into the parent interface).

    Thanks for your answers !

  • To show the widget multiple times, you don't need to load the plugin multiple times. That just doesn't make any sense. Just have your plugin's factory method produce a new instance of your widget.

  • But, do we agree that each time user want to get access to the plugin, I have to do this :

    QPluginLoader loader(pluginToLoadFilePath);
    QObject *pluginInstance = loader.instance();
    IPlugin *plugin = qobject_cast<IPlugin *>(pluginInstance);

    // The interface has a virtual function getWidget()
    // returning the widget associated with this plugin.
    // Moreover, the plugin has a function setData()
    // to set the data to show.


    It works well the first time, but if user wants to use the same plugin with another kind of data (it means another plugin).

    I just had this idea : shall it work if in the function getWidget(), instead of returning a pointer on the current object deriving either from QWidget and from the plugin interface, I return a new widget ?

    I forgot to precise english is not my maternal language, so excuse my poor english (including comprehension).

    Thanks a lot.

  • No, we don't. You just load the plugin once. You only need QPluginLoader that once. You just ask for the instance for than once. Or, better yet, you have your plugin not return the actual class that you want, but a factory that can create instances of that class. Then, if you need another instance, you simply ask the factory to produce one for you.

  • Moderators

    The pluginloader loads code into your program. All code is identified by symbol names (your methods, etc. with a bit of added name mangling to encode type information when using C++). You need to make sure the right symbols are found whenever you need to access one of them (e.g. you call a method).

    So for a plugin to load several times you would need to make sure all its symbols are changed before loading it a second time. Otherwise the symbols of one of the plugins will be hidden behind those of the other which will most likely cause some really interesting effects.

    So, no you can not load the same plugin twice. You can use the code of one plugin several times though.

Log in to reply