Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Is it possible to load QML libraries at runtime like Qt Plugins are loaded with QPluginLoader?
Forum Updated to NodeBB v4.3 + New Features

Is it possible to load QML libraries at runtime like Qt Plugins are loaded with QPluginLoader?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 601 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.
  • F Offline
    F Offline
    fama_lab3d
    wrote on last edited by
    #1

    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:

    1. Create the main window that will act as a container
    2. Load all the plugins that I can find
    3. Each plugin will implement an interface that gives me the model it supports
    4. The plugin will implement the logic to communicate with the machine, and also provide the UI
    5. 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 use qt_add_plugin to 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 from QQmlEngineExtensionPlugin instead 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);
    }
    
    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Yes. You can load. Just call registerTypes() once you load through QPluginLoader.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      F 1 Reply Last reply
      0
      • dheerendraD dheerendra

        Yes. You can load. Just call registerTypes() once you load through QPluginLoader.

        F Offline
        F Offline
        fama_lab3d
        wrote on last edited by
        #3

        @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...

        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