Results 1 to 14 of 14

Thread: Problem with Qt plugin system

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Qt plugin system

    Quote Originally Posted by zuck View Post
    if I need to share some concrete methods (which the pkServer instance needs to know too) with all applications derived from pkApplication interface? How can I do that?
    it is recommended to avoid that as much as possible. Your plugin system should be designed (mostly) one-way : the application call methods from the plugin and the plugin acts on its own.
    For the cases where it is really needed, there are two main ways :

    • passing pointers to "controllers" object (recommened method in most cases, more on this below)
    • putting all the common code in a shared lib. both the app and the plugins would link against it

    A controller object is basically a class that allows the plugin to obtain data or to perform actions in a way that is not "predictable" by the app.

    Just create another abstract class exposing methods of interest (which must be pure virtual). Then subclass that class in your app to provide actual implementation of these methods and pass a pointer to an object of your controller class to the plugins. e.g :

    Qt Code:
    1. class pkApplicationController
    2. {
    3. public:
    4. virtual ~pkApplicationController() {}
    5.  
    6. virtual QString statusMessage() const = 0;
    7. virtual void setStatusMessage(const QString& s) = 0;
    8. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class pkConcreteAppController : public pkApplicationController
    2. {
    3. public:
    4. QString statusMessage() const { return m_status; }
    5. void setStatusMessage(const QString& s) { m_status = s; }
    6. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // somewhere in your app : (pointerToController is a pkConcreteAppController*)
    2. plugin->someMethod(pointerToController);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // in a plugin
    2. void SomePlugin::someMethod(pkApplicationController *c)
    3. {
    4. if ( !happy )
    5. c->setStatusMessage("Always look at the bright side of life.");
    6. }
    To copy to clipboard, switch view to plain text mode 

    I hope this helps.
    Current Qt projects : QCodeEdit, RotiDeCode

  2. The following user says thank you to fullmetalcoder for this useful post:

    zuck (19th March 2009)

  3. #2
    Join Date
    Mar 2009
    Posts
    72
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Qt plugin system

    Yes, this is a solution.

    What about using QLibrary instead of QPluginLoader? With QLibrary can I have an abstract base class (no pure virtual methods only) as "parent" for all derived applications?
    Last edited by zuck; 19th March 2009 at 22:10.

  4. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Qt plugin system

    using QLibrary instead of QPluginLoader offers a single avantage : more control. QPluginLoader actually uses a QLibrary under the hood so the same restrictions apply. Besides, using QLibrary implies recreating what QPluginLoader provides (you cannot create objects directly from shared libs, you have to export a "creator" functions which must have a name known to the plugin loading code, must be mangled properly, ...)

    Just to clarify about the requirements on a "base class" for plugins (provided you don't use a "common shared lib" approach) :

    • such a class does not need to be pure virtual (aka abstract) stricto sensu (it can inherit non-abstract classes for instance)
    • it must however be pure virtual in a "practical sense", which basically means that the only methods it brings (on its own, see previous remark) must be pure virtual and it cannot have member variables of its own

    An important consequence is that such a class can inherit QObject for instance but then the Q_OBJECT macro must be omitted as it adds members/methods used by the meta object system.
    Current Qt projects : QCodeEdit, RotiDeCode

  5. #4
    Join Date
    Mar 2009
    Posts
    72
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Qt plugin system

    So I can write the inverse of original situation:

    Qt Code:
    1. class pkApplicationBase : public QObject
    2. {
    3. // The id logic here (without Q_OBJECT macro).
    4. };
    5.  
    6. class pkApplication : public pkApplicationBase
    7. {
    8. // abstract interface here.
    9. };
    To copy to clipboard, switch view to plain text mode 

    Is it correct?

  6. #5
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Qt plugin system

    No. You can afford to use "semi-abstract" classes as base class for plugins because both your app and the plugin links to Qt libs dynamically (or any other shared lib providing classes that you could be tempted to use as ancestors of your plugin base class).

    Any class that brings at least one method/member variable of its very own (i.e not through inheritance of a class defined in a common shared lib) is definitely unreachable from plugins (unless, again, you move it to a common shared lib).

    Please not that the shared is equally important as the common. You may manage to get it working in some cases when compiling a common lib statically but it is extremely likely to fail miserably or a reason or another in a vast majority of cases.

    In any case you're safer using only (totally) pure virtual classes in your plugin system and, if needed, a bit of meta-object magic here and there.
    Current Qt projects : QCodeEdit, RotiDeCode

  7. #6
    Join Date
    Mar 2009
    Posts
    72
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Qt plugin system

    So the correct procedure is:

    1) Put pkApplication and pkApplicationBase in a shared lib.
    2) Link ExampleApp and pkServer modules to the shared lib.

    ...Right?

Similar Threads

  1. Threads in Designer plugins
    By hvengel in forum Qt Tools
    Replies: 2
    Last Post: 3rd January 2009, 19:19
  2. QtPlugins how to communicate between plugins?
    By BeS in forum Qt Programming
    Replies: 1
    Last Post: 17th December 2008, 11:59
  3. Container plugins
    By Benne Gesserit in forum Qt Tools
    Replies: 4
    Last Post: 20th November 2008, 23:43
  4. Replies: 1
    Last Post: 31st October 2008, 14:35
  5. Qt Plugins Error
    By nathanpackard in forum Qt Programming
    Replies: 1
    Last Post: 17th August 2007, 23:19

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.