Important: Please read the Qt Code of Conduct -

Global function access to all plugins

  • How can I make a Global function which can be accessed by my QApplication(main.cpp where all my Plugins are loaded) and to all Qt Plugins my QApplication loads ?
    I am relatively new to Qt Plugins so an example would be nice.

  • Lifetime Qt Champion


    What do you need such a global function for ?

  • I wish to make a convenience function which reads from a QHash that contains (QString, Plugin QObject*) pair, and returns the QObject* for the string passed as argument

  • Lifetime Qt Champion

    And what use would the plugin have if this function ?

  • To get the QObject instance of other plugins, via the central repo of objects(QHash(QString, QObject*))

  • Lifetime Qt Champion

    That sounds a bit strange. If I understand correctly, you want your plugins to access other plugins but they might not be loaded. Can you explain the goal of your design ?

  • @SGaist
    In my main.cpp I am loading the Plugins in a specific order so that, if I want to use a QObject Instance of one plugin, it would have already been loaded before I use it in another plugin.
    Basically I am trying to create a way to manage the QObject instance of plugins, such that I can access them by simply access any of them by passing their name(QString in the hash) from other plugins.

  • Lifetime Qt Champion

    Plugins should not rely on each other -esp. not the way you're doing it. If a plugin needs another plugin than it should be resolved inside the plugin, not externally.

  • Heres an example of what I am trying to do:
    I have 1 plugin (say View plugin) which has a qml being loaded into a QQuickItem, where I am making a custom graph plot. Relating to this I have a model which supplies me with data for points to plot (in another Plugin, say Model Plugin).
    Like this I have quite a few View and model plugins and my main.cpp loads them all using QPLuginLoader and arranges them in a layout defined in main.qml(loaded by a QQuickView).
    So, I need these model and View Plugin's QObject instance to be available to other plugins. That is why I was trying to make a global class/function which could handle this.

  • Lifetime Qt Champion

    You should rather have an interface on your plugins that allows to configure them as needed rather than relying on an arbitrary order from some external source and your plugins having to know stuff they should not have to care for.

  • @SGaist I dont understand how will I be able to access other plugin objects using such interface.

  • Currently I have used the following work around:
    1)Load all the plugins in main.cpp.
    2)Use the instance() function to get the base QObject instance of each plugin. Register these objects with an object name.
    3)Since qApp is a global pointer to QApplication instance, make all QObjects a child of qApp.
    4)use qApp pointer in all plugins to get any plugin instance i need, using findChildren().
    5)I use an interface file to cast the QObject into a type which the base class of the plugin inherits from and then I am able to use all funtions from the interface, whose definition are on that plugin.
    6)Also in order to take care of Plugin loading readiness. I have made a signal, which I call at the end of main.cpp, after all plugins have been loaded and connected to slot of all plugins defined in interface. So, all actions that require other plugins to be ready are performed in this slot of each plugin

    This works for me but I was looking for a neater alternative. Alternate to using qApp, I came up with this idea about a global class / pointer / function /variable.
    What is your comment on this @SGaist ?

  • Lifetime Qt Champion

    That you have a design issue.

    If you need your plugins to know a lot of stuff about other plugins then clearly there's something wrong going on.

    Are you sure that you need plugins at all ?

    The role of a plugin is to extend a functionality and if it's not there then the extension is simply not available. Here you have them so tightly coupled that it defeats the goal of the plugin itself.

  • @SGaist according to
    Qt provides two APIs for creating plugins:

    A high-level API for writing extensions to Qt itself: custom database drivers, image formats, text codecs, custom styles, etc.
    A low-level API for extending Qt applications.

    So I am making lower level APIs. And my plugins are extending my main Qt Application. Only the thing is PLugins may have dependency on each other too

    Basically I want to achieve collaborative development using Plugins, where each developer can work on his Qt plugin. And one plugin can have dependency on another.

  • Isnt Qt Plugins a good way to achieve collaborative development ?

  • Lifetime Qt Champion

    @Luckless said in Global function access to all plugins:

    Isnt Qt Plugins a good way to achieve collaborative development ?

    Can't you simply use shared libraries instead of plug-ins?

  • Actually Earlier I was using Shared Libraries. But Later I came across QT Plugins, and the instance() function that QPluginLoader provides to get the object of root node of the plugin, which I believe is not provided by QLibrary.
    For this reason I went for Plugins over simple Shared Libraries. Is that bad @jsulm ?

  • @Luckless Sounds like singleton class. Create your own. Do what you want inside.

  • Lifetime Qt Champion

    @Luckless said in Global function access to all plugins:

    Is that bad @jsulm ?

    The question is rather: do you need plug-ins? Plug-ins are like shared libraries but are loaded at runtime when activated (just like what QtCreator is doing). And as pointed out by others they should not depend on each other. So, if your plug-ins are always needed by your application then I don't see the point to implement them as plug-ins, make them normal shared libraries.

  • Lets say I revert back to my Shared library / QLibrary code.
    How would I achieve the following:

    @Luckless said in Global function access to all plugins:

    To get the QObject instance of other plugins, via the central repo of objects(QHash(QString, QObject*))

    And would it be any different if I try to achieve this in either Plugin or Shared Library case

  • Lifetime Qt Champion

    @Luckless If you do not use plug-ins there is no need to get instances of other plug-ins.
    If you need use functionality of lib a in lib b then do it in the usual way: include a header file in b and use the functionality.

Log in to reply