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. How do I instantiate many object instance with QPluginLoader?
Forum Updated to NodeBB v4.3 + New Features

How do I instantiate many object instance with QPluginLoader?

Scheduled Pinned Locked Moved Solved General and Desktop
qpluginloader
3 Posts 2 Posters 1.4k 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
    FloatFlower.Huang
    wrote on last edited by FloatFlower.Huang
    #1

    In Qt documentation(http://doc.qt.io/qt-5/qpluginloader.html):

    Returns the root component object of the plugin. The plugin is loaded if necessary. The function returns 0 if the plugin could not be loaded or if the root component object could not be instantiated.
    If the root component object was destroyed, calling this function creates a new instance.

    Is it means that each QPluginLoader can only instantiate one object? How do I create lots of instance from QPluginLoader?

    In source code, implementation of instance() :

    QObject *QPluginLoader::instance()
    {
        if (!isLoaded() && !load())
            return 0;
        if (!d->inst && d->instance)
            d->inst = d->instance();
        return d->inst.data();
    }
    

    d is a pointer point to QLibraryPrivate, which keep a QPointer<QObject> inst; point to the object that create from instance(), so I think the object create from QPluginLoader is always singleton, how can I create many different objects from it?

    The only solution I figure out:

    class Foo : public QObject
    {
        Object* creator() {
            return new Foo();
        }
    }
    

    and I can do this:

    QPluginLoader loader("PATH/TO/LOADER");
    QObject* object = loader.instance();
    Foo* foo = qobject_cast<Foo*>(object);
    Foo *foo1 = foo->creator();
    

    I don't know whether the solution is a good solution or not.

    jsulmJ 1 Reply Last reply
    0
    • F FloatFlower.Huang

      In Qt documentation(http://doc.qt.io/qt-5/qpluginloader.html):

      Returns the root component object of the plugin. The plugin is loaded if necessary. The function returns 0 if the plugin could not be loaded or if the root component object could not be instantiated.
      If the root component object was destroyed, calling this function creates a new instance.

      Is it means that each QPluginLoader can only instantiate one object? How do I create lots of instance from QPluginLoader?

      In source code, implementation of instance() :

      QObject *QPluginLoader::instance()
      {
          if (!isLoaded() && !load())
              return 0;
          if (!d->inst && d->instance)
              d->inst = d->instance();
          return d->inst.data();
      }
      

      d is a pointer point to QLibraryPrivate, which keep a QPointer<QObject> inst; point to the object that create from instance(), so I think the object create from QPluginLoader is always singleton, how can I create many different objects from it?

      The only solution I figure out:

      class Foo : public QObject
      {
          Object* creator() {
              return new Foo();
          }
      }
      

      and I can do this:

      QPluginLoader loader("PATH/TO/LOADER");
      QObject* object = loader.instance();
      Foo* foo = qobject_cast<Foo*>(object);
      Foo *foo1 = foo->creator();
      

      I don't know whether the solution is a good solution or not.

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

      @FloatFlower.Huang Your root object (Foo in the example) should create the instances you need.
      See here http://doc.qt.io/qt-5/plugins-howto.html

      class MyStylePlugin : public QStylePlugin
      {
          Q_OBJECT
          Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QStyleFactoryInterface" FILE "mystyleplugin.json")
      public:
          QStyle *create(const QString &key);
      };
      

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

      1 Reply Last reply
      3
      • F Offline
        F Offline
        FloatFlower.Huang
        wrote on last edited by
        #3

        @jsulm Thanks a lot!
        Sorry, I didn't read the documentation carefully.

        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