Results 1 to 4 of 4

Thread: Multiple instance of Plugin object

  1. #1
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Multiple instance of Plugin object

    Hi,

    can I instantiate multiple object of a plugin class?
    I show you a prototype of code I'd like to use

    Qt Code:
    1. QPluginLoader loader (pluginDir.absoluteFilePath (fileName));
    2.  
    3. QObject *instance = loader.instance ();
    4. QObject *newObj = 0;
    5. QMetaObject *mo = 0;
    6. if (instance) {
    7. IExample *plugin = qobject_cast<IExample*>(instance);
    8. if (plugin) {
    9. qDebug() << instance->metaObject ()->className () << "plugin says:"
    10. << plugin->messageString ();
    11.  
    12. newObj = instance->metaObject ()->newInstance (0);
    13. if (0 != newObj)
    14. qDebug () << "Create New Instance OK";
    15. else
    16. qDebug () << "Create New Instance Failed";
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    where IExample is defined

    Qt Code:
    1. class IExample
    2. {
    3. public:
    4. virtual ~IExample () {}
    5.  
    6. virtual QString messageString () const = 0;
    7. };
    8.  
    9. Q_DECLARE_INTERFACE (IExample, "local.costa.IExample/1.0")
    To copy to clipboard, switch view to plain text mode 

    and this is the loaded plugin

    Qt Code:
    1. class HelloWorldPlugin: public QObject, IExample
    2. {
    3. Q_OBJECT
    4. Q_INTERFACES (IExample)
    5.  
    6. public:
    7. HelloWorldPlugin(QObject *parent = 0);
    8.  
    9. QString messageString () const;
    10. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. HelloWorldPlugin::HelloWorldPlugin(QObject *parent)
    2. : QObject (parent)
    3. {
    4. }
    5.  
    6. QString HelloWorldPlugin::messageString() const
    7. {
    8. return QString("Hello World");
    9. }
    10.  
    11. Q_EXPORT_PLUGIN2 (HelloWorldPlugin, HelloWorldPlugin)
    To copy to clipboard, switch view to plain text mode 

    the output is

    Qt Code:
    1. HelloWorldPlugin plugin says: "Hello World"
    2. Create New Instance Failed
    3. Found 1 entries
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  2. #2
    Join Date
    Aug 2008
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple instance of Plugin object

    It says in Qt's documentation at QMetaObject::newInstance() this:
    Note that only constructors that are declared with the Q_INVOKABLE modifier are made available through the meta-object system.
    I don't see this Q_INVOKABLE macro used in your source code. Try adding it and see what happens.

  3. The following user says thank you to joyer83 for this useful post:

    mcosta (15th June 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple instance of Plugin object

    Usually you make the plugin a factory that can return instances of interfaces you need:

    Qt Code:
    1. class MyPluginIface : ... {
    2. // ...
    3. public:
    4. virtual MyRealIface * create() = 0;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Then you can create as many instances of MyRealIface as you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    mcosta (15th June 2011)

  6. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple instance of Plugin object

    Thank you for the answer.

    Your solution is more elegant than than mine.
    I was courious about QMetaObject::newInstance().

    I solved using joyer83 tips but for a real solution I'll use the factory.
    Last edited by wysota; 15th June 2011 at 17:39.
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Replies: 1
    Last Post: 13th April 2011, 23:08
  2. Replies: 5
    Last Post: 5th July 2010, 08:19
  3. Replies: 0
    Last Post: 12th November 2009, 18:47
  4. Object instance in a singleton class dissapears
    By vieraci in forum Qt Programming
    Replies: 2
    Last Post: 9th August 2009, 23:51
  5. Open multiple files with one program instance
    By Ginsengelf in forum Qt Programming
    Replies: 6
    Last Post: 16th March 2009, 14:08

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.