Results 1 to 8 of 8

Thread: Qt plugin: access application classes from the plugin?

  1. #1
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Qt plugin: access application classes from the plugin?

    Hello all,

    Please consider the following mock-up of how my application works:
    Qt Code:
    1. class DataSource
    2. {
    3. public:
    4. QVariant dataForKey(const QString &key);
    5. };
    6.  
    7.  
    8. class BaseWidget : public QWidget
    9. {
    10. Q_OBJECT
    11. public:
    12. BaseWidget(QWidget* parent);
    13. void setDataSource(DataSource* ds) { this->ds = ds; }
    14. virtual void draw(QPainter* painter) = 0;
    15. protected:
    16. DataSource* ds;
    17. };
    18.  
    19. class SomeWidget : public BaseWidget
    20. {
    21. public:
    22. virtual void draw(QPainter* painter)
    23. {
    24. // do some custom drawing using ds->dataForKey()
    25. // with specific "key" parameter
    26. }
    27. };
    To copy to clipboard, switch view to plain text mode 

    My application has a repertoire of subclasses of BaseWidget that I would like to dynamically extend through plugins. Qt's documentation is quite clear on the basics, but covers only the case where the plugin can be implemented exclusively with pure Qt facilities. In this case, I would like the plug-in to be able to access my custom facilities, i.e. the DataSource mechanism in the mock-up above.

    I'm not sure how to proceed toward the implementation of such a setup. How can I provide the plugins with access to facilities sitting in the main application? Should I put these facilities in a separate dynamic library against which both the application and the plugins will be linked? Or is there a potentially simpler way to achieve this?

    Thanks in advance for your help,
    Antoine

  2. #2
    Join Date
    May 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt plugin: access application classes from the plugin?

    Hi!

    I had exactly the same question...

    Did you find a good solution?

    Or do people have some idea?
    (for example, can we declare an interface in the main application ?)

    Thanks a lot
    Cheers,
    Benoit

  3. #3
    Join Date
    Jul 2007
    Posts
    121
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt plugin: access application classes from the plugin?

    You are exporting SomeWidget class in your plugin to the application, aren't you?

    If yes, then it is not that "application has a repertoire of subclasses of BaseWidget ", but a plugin has them.

    In the mockup that you provided it looks like that any class in your plugin that implements BaseWidget interface will have an access to DataSource. But, as far as I know, only one class can be exported from the plugin, except multiple inheritance construct of type (then you can cast returned "instance" it to the base types)

    class ExportedWidget: public SomeWidget1, public SomeWidget2, public BaseWidget
    {
    ..
    Q_INTERFACES(BaseWidget)
    ..
    }

  4. #4
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt plugin: access application classes from the plugin?

    Quote Originally Posted by QPlace View Post
    You are exporting SomeWidget class in your plugin to the application, aren't you?

    If yes, then it is not that "application has a repertoire of subclasses of BaseWidget ", but a plugin has them.
    Agreed.

    Quote Originally Posted by QPlace View Post
    In the mockup that you provided it looks like that any class in your plugin that implements BaseWidget interface will have an access to DataSource. But, as far as I know, only one class can be exported from the plugin, except multiple inheritance construct of type (then you can cast returned "instance" it to the base types)
    I'm not sure I understand your point. I agree that my mockup lacks the Qt-isms specific to plug-ins. And yes, I don't expect plug-in to export more than a single class. The question was about the use in the plugin of facilities sitting in the application.

  5. #5
    Join Date
    May 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt plugin: access application classes from the plugin?

    By reading a lot of posts on the forum it seems that the best way is to use a shared library for the functions used by both the plugins and the application.

    Ben

  6. #6
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt plugin: access application classes from the plugin?

    Quote Originally Posted by be-noix View Post
    By reading a lot of posts on the forum it seems that the best way is to use a shared library for the functions used by both the plugins and the application.

    Ben
    I see. Out of curiosity, can you think of another way?

  7. #7
    Join Date
    May 2009
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Qt plugin: access application classes from the plugin?

    Function defined in your interface:

    Qt Code:
    1. virtual void Initialize(int *AHostClass) = 0;
    To copy to clipboard, switch view to plain text mode 

    When you call it after load the plugin you should send the class pointer like this (this for the caller class, or any other class pointer):

    Qt Code:
    1. PluginLoader_Class_Object->Initialize(reinterpret_cast<int*>(this));
    To copy to clipboard, switch view to plain text mode 

    Inside of the plugin you shoul convert this int to a class again creating a class pointer and asignating this value to it.

    Qt Code:
    1. void Initialize(int *AHostClass)
    2. {
    3. TCLASS *HostClassInsidePlugin;
    4. HostClassInsidePlugin = reinterpret_cast<TCLASS *>(AHostClass);
    5. HostClassInsidePlugin->... any member of the class passed.
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    Remember thath you shoud have the same exactli header .h from the host application insite of you plugin proyect.
    Last edited by masterbraind; 5th August 2011 at 15:49. Reason: missing [code] tags

  8. #8
    Join Date
    Apr 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt plugin: access application classes from the plugin?

    It is possible to call back from plugin into application using pointer to function:

    Qt Code:
    1. // plugin
    2. void MyPlugin::someFunctionInPlugin(int (*functionInApp)())
    3. {
    4. int result = (*functionInApp)();
    5. }
    6.  
    7. // application code
    8. int someFunctionInApp()
    9. {
    10. ...
    11. }
    12. void someOtherFunctionInApp()
    13. {
    14. plugin->someFunctionInPlugin(&someFunctionInApp);
    15. }
    To copy to clipboard, switch view to plain text mode 

    Member function of class defined in application might be called from plugin if this function is virtual (at least it may compile without errors), or when it is passed via pointer to member function:

    Qt Code:
    1. // plugin
    2. void MyPlugin::someFunctionInPlugin(ClassInApp *obj, void (*ClassInApp::Fun)())
    3. {
    4. obj->virtualFunction();
    5. ((*obj).*(Fun))();
    6. }
    7.  
    8. // application
    9. class ClassInApp
    10. {
    11. virtual void virtualFunction();
    12. void someMemberFunction();
    13. };
    14.  
    15. void someFunctionInApp()
    16. {
    17. ClassInApp obj;
    18. plugin->someFunctionInPlugin(&obj, &ClassInApp::someMemberFunction);
    19. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. directfb plugin of qte.4.4.3 can not be loaded when running application
    By xianshuiren in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 15th February 2009, 06:54
  2. Plugin implementation question
    By JPNaude in forum Qt Programming
    Replies: 12
    Last Post: 27th August 2008, 21:24
  3. What about a plugin application?
    By Raccoon29 in forum Newbie
    Replies: 4
    Last Post: 16th March 2008, 17:36
  4. QPluginLoader not recognizing a plugin
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 29th June 2007, 15:13
  5. Application plugin on windows
    By Eyee in forum Qt Programming
    Replies: 2
    Last Post: 22nd March 2006, 18:36

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.