Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Application Plugins with the same interface
Forum Updated to NodeBB v4.3 + New Features

Qt Application Plugins with the same interface

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 828 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W wasawi2

    Hello everyone,

    I am following the echoplugin example to understand how to build a structure of plugins for my application.

    In my case I think my set of plugins would share the same interface. My question is how to set an interface class that would be used by multiple plugins?

    I currently have this interface which is the similar to the one in the example.

    #ifndef WIDGETS_INTERFACE_H
    #define WIDGETS_INTERFACE_H
    
    #include <QObject>
    
    //! [0]
    class Widgets_interface
    {
    public:
        virtual ~Widgets_interface() = default;
        virtual QWidget* open(QWidget* parent) = 0;
        virtual void close() = 0;
    };
    
    QT_BEGIN_NAMESPACE
    
    #define Widgets_interface_iid "Widgets_interface"
    
    Q_DECLARE_INTERFACE(Widgets_interface, Widgets_interface_iid)
    QT_END_NAMESPACE
    
    //! [0]
    #endif
    

    If i load this interface in the main application I obviously have no way to differentiate which plugin am I loading.

    bool EchoWindow::loadWidget02Plugin()
    {
        QDir pluginsDir(QCoreApplication::applicationDirPath());
    #if defined(Q_OS_WIN)
        if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
            pluginsDir.cdUp();
    #elif defined(Q_OS_MAC)
        if (pluginsDir.dirName() == "MacOS") {
            pluginsDir.cdUp();
            pluginsDir.cdUp();
            pluginsDir.cdUp();
        }
    #endif
        pluginsDir.cd("plugins");
        const QStringList entries = pluginsDir.entryList(QDir::Files);
        for (const QString& fileName : entries) {
            QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
            QObject* plugin = pluginLoader.instance();
            if (plugin) {
                widget02_interface = qobject_cast<Widgets_interface*>(plugin);
                if (widget02_interface)
                    return true;
                pluginLoader.unload();
            }
        }
    
        return false;
    }
    

    Does this mean I need to have a different header/interface file for each plugin even if they share the same functionality?

    Sorry for my very basic question. I'm still learning.

    Thank you,
    w

    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #3

    @wasawi2 said in Qt Application Plugins with the same interface:

    if they share the same functionality?

    Why would two plugins do the same for different types? What's the use-case?

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    W 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      @wasawi2 said in Qt Application Plugins with the same interface:

      if they share the same functionality?

      Why would two plugins do the same for different types? What's the use-case?

      W Offline
      W Offline
      wasawi2
      wrote on last edited by
      #4

      @Christian-Ehrlicher thank you for asking.

      Actually Im not even sure using plugins is the best solution for my use case.

      My idea it was to have a launcher application that loads a set of plugins (10 to 20) and each of them loads its own UI and has different functionalities. The launcher application only needs to call open or close to each plugin, so the interface is the same for all of them. This way I can have some advantages in maintenance, I can work in one plugin knowing that I don't have to recompile and ship the entire application but only one of its plugins. This may introduce some complexity when it comes to keeping track of each plugin version, but that is not a problem for now..

      So for what I understand the interface should be the same for all of them. Should i have a different class name and header file for each plugin? even if the open and close will be the same for all of them?

      Thank you,
      w

      1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        The interface stays the same. What can change is what you use for the implementation. In the example you show, there's a different handling between macOS and Windows. In some circonstances, you may have one implementation file per platform but it's nothing unusual nor overly complex to manage.

        W Offline
        W Offline
        wasawi2
        wrote on last edited by
        #5

        @SGaist said in Qt Application Plugins with the same interface:

        The interface stays the same. What can change is what you use for the implementation.

        Ok, so the open and close functions remain the same but the class name, the #define and the Q_DECLARE_INTERFACE has to be different?
        So I need a different header file for each plugin?

        Sorry for my very basic question.. I'm a bit confused.

        Thank you,
        w

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #6

          Check for example the Qt SQL plugins, that might be simpler to get the structure right.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          W 1 Reply Last reply
          0
          • SGaistS SGaist

            Check for example the Qt SQL plugins, that might be simpler to get the structure right.

            W Offline
            W Offline
            wasawi2
            wrote on last edited by
            #7

            @SGaist thank you! But I can not find any Qt SQL plugins example in the example folder... I'm on Qt 6.5.2.

            I had a look at the plugandpaint example but that didn't help much.

            jsulmJ 1 Reply Last reply
            0
            • W wasawi2

              @SGaist thank you! But I can not find any Qt SQL plugins example in the example folder... I'm on Qt 6.5.2.

              I had a look at the plugandpaint example but that didn't help much.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @wasawi2 I think @SGaist means to check the implementation of SQL plug-ins (source code) to see how it is done there

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              Christian EhrlicherC SGaistS 2 Replies Last reply
              0
              • jsulmJ jsulm

                @wasawi2 I think @SGaist means to check the implementation of SQL plug-ins (source code) to see how it is done there

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #9

                I still don't see the usecase here. Why one plugin with two different interfaces? The sql plug-ins for sure don't make such hacks - I would be aware of (and fixed it) :)

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @wasawi2 I think @SGaist means to check the implementation of SQL plug-ins (source code) to see how it is done there

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @jsulm said in Qt Application Plugins with the same interface:

                  @wasawi2 I think @SGaist means to check the implementation of SQL plug-ins (source code) to see how it is done there

                  Exactly that

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SamiV123
                    wrote on last edited by SamiV123
                    #11

                    Why make a plugin system anyway? Do you have an ecosystem with 3rd party developers providing features for your app?

                    If not then why complicate things? Making a robust plugin system with interfaces, versioning, correct loading unloading and handling transient dependencies to Qt and CRT and any other library and dealing with correct runtime resource management is just a ton of complications. Also your build files get more complicated with more build targets and more build rules.

                    Ideal code base has nothing in it.
                    0 lines of code, 0 lines of build rules, 0 bugs, 0 headaches.

                    From the engineering perspective any line of code you add, any build target you add, any library you add can only server to make things worse for you.

                    W 1 Reply Last reply
                    0
                    • S SamiV123

                      Why make a plugin system anyway? Do you have an ecosystem with 3rd party developers providing features for your app?

                      If not then why complicate things? Making a robust plugin system with interfaces, versioning, correct loading unloading and handling transient dependencies to Qt and CRT and any other library and dealing with correct runtime resource management is just a ton of complications. Also your build files get more complicated with more build targets and more build rules.

                      Ideal code base has nothing in it.
                      0 lines of code, 0 lines of build rules, 0 bugs, 0 headaches.

                      From the engineering perspective any line of code you add, any build target you add, any library you add can only server to make things worse for you.

                      W Offline
                      W Offline
                      wasawi2
                      wrote on last edited by
                      #12

                      @SamiV123 this is a very good point. I will consider avoiding plugins at all costs. Maybe it is a better idea to forget about the launcher application and just make separate applications.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved