Results 1 to 16 of 16

Thread: creating QWidget Plugins

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default creating QWidget Plugins

    I want to create QWidget plugins for an application, that implement a certain interface. Those plugins are simply custom views that implement that interface and the main application loads these widgets into the main view.

    However, I am not sure how to do this efficiently. I know that the Plugin itself has to be of the form
    Qt Code:
    1. class MyPlugin : public QObject, public MyPluginInterface
    2. {
    3. Q_OBJECT
    4. Q_INTERFACES( MyPluginInterface )
    5. ...
    6. };
    To copy to clipboard, switch view to plain text mode 

    I read that, if I want to actually create a QWidget from this plugin, the interface should simply contain
    Qt Code:
    1. class MyPluginInterface
    2. {
    3. ...
    4. QWidget* createWidget(QWidget* parent) = 0;
    5. ...
    6. };
    To copy to clipboard, switch view to plain text mode 

    And thus there would be another class
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. ...
    4. };
    To copy to clipboard, switch view to plain text mode 

    But how would I let the actual Widget itself interact with the application? Wouldn't I need to make a wrapper function in the plugin for every single function of the interface, so that it is passed to the Widget object?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: creating QWidget Plugins

    But how would I let the actual Widget itself interact with the application? Wouldn't I need to make a wrapper function in the plugin for every single function of the interface, so that it is passed to the Widget object?
    No.
    Qt Code:
    1. //in your application code
    2. MyWidget *pWidget = m_pPlugin->createWidget();
    3. if(pWidget){
    4. pWidget->someMethod();
    5. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: creating QWidget Plugins

    Quote Originally Posted by high_flyer View Post
    No.
    Qt Code:
    1. //in your application code
    2. MyWidget *pWidget = m_pPlugin->createWidget();
    3. if(pWidget){
    4. pWidget->someMethod();
    5. }
    To copy to clipboard, switch view to plain text mode 
    Well, yeah, I guess it was kind of a stupid question . I am simply a bit confused on how to implement this the "correct" way. So if I want to create plugins that are independent views in the main application, with which the main application interfaces through a certain interface (i.e. ViewInterface). Would I need to do something like this?

    Qt Code:
    1. class ViewPlugin : public QObject, public ViewPluginInterface
    2. {
    3. Q_OBJECT
    4. Q_INTERFACES( ViewPluginInterface )
    5. ...
    6. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class ViewPluginInterface
    2. {
    3. ViewWidget* createWidget( QWidget* parent ) = 0;
    4. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class ViewWidget : public ViewInterface
    2. {
    3. ...
    4. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class ViewInterface : public QWidget
    2. {
    3. ...
    4. };
    To copy to clipboard, switch view to plain text mode 

    In that case, the createWidget function would be the only function of the plugin interface. And I am not sure the inheritance of QWidget works this way?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: creating QWidget Plugins

    In that case, the createWidget function would be the only function of the plugin interface.
    The code you post would work, but make little sense as a plugin - since in what way the fact the widget came from a plugin would be different then using new MyWidget() in your application?

    It makes sens to use a plugin, when the objects the various plugins provide conform to one interface.
    So in your application you don't need to know which class implements the interface, and just use the interface methods - that is the whole idea (very simplified) of using a plugin.
    So if you can contain the functionality of all you plugin provided widgets in a fixed set of methods, then you expose those methods through the interface and use an interface pointer to call them, without caring which class currently implemtns it.
    Qt Code:
    1. class MyPluginInterface
    2. {
    3. MyWidget* createWidget(QWidget* parent) = 0;
    4. void someMethod() = 0;
    5. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //in your applicaiton
    2. MyWidget *pView = m_pPlugin->createWidget();
    3. if(pView){
    4. pView->someMethod();
    5. }
    To copy to clipboard, switch view to plain text mode 

    This brings us back to your previous post.
    And yes, you will have to expose all the methods shared by your plugin widgets through the interface.
    This is what the interface is for.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: creating QWidget Plugins

    Quote Originally Posted by high_flyer View Post
    So if you can contain the functionality of all you plugin provided widgets in a fixed set of methods, then you expose those methods through the interface and use an interface pointer to call them, without caring which class currently implemtns it.
    Yes, that is the plan at least.


    Quote Originally Posted by high_flyer View Post
    Qt Code:
    1. class MyPluginInterface
    2. {
    3. MyWidget* createWidget(QWidget* parent) = 0;
    4. void someMethod() = 0;
    5. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //in your applicaiton
    2. MyWidget *pView = m_pPlugin->createWidget();
    3. if(pView){
    4. pView->someMethod();
    5. }
    To copy to clipboard, switch view to plain text mode 

    This brings us back to your previous post.
    And yes, you will have to expose all the methods shared by your plugin widgets through the interface.
    This is what the interface is for.
    Hm, sorry, I don't understand your example . In this example, wouldn't the MyPlugin and the MyWidget class have to implement the MyPluginInterface? And wouldn't the implementation of someMethod() be empty in MyPlugin and implemented in MyWidget, while the createWidget() function would be the other way around?

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: creating QWidget Plugins

    Hm, sorry, I don't understand your example . In this example, wouldn't the MyPlugin and the MyWidget class have to implement the MyPluginInterface?
    There is no MyPlugin class.
    m_pPlugin in my example is:
    Qt Code:
    1. MyPluginInterface *m_pPlugin;
    To copy to clipboard, switch view to plain text mode 
    And yes, your class needs to implement the interface, since it will derive from it.

    you can do the following instead of what you did:
    Qt Code:
    1. class ViewPluginWidget : public QWidget, public ViewPluginInterface
    2. {
    3. Q_OBJECT
    4. Q_INTERFACES( ViewPluginInterface )
    5. ...
    6. };
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: creating QWidget Plugins

    Ok, I think my confusion came from the fact, that I thought the plugin must somehow inherit from QObject and can't be just QWidget only . However, in that case I don't really need a createWidget function?

    I was thinking of doing something like this now:
    Qt Code:
    1. class ViewPluginInterface : public QWidget // to ensure, that anyone using this interface, is creating it as a QWidget
    2. {
    3. ...
    4. };
    5.  
    6. Q_DECLARE_INTERFACE( ViewPluginInterface, "..." )
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class SomeCustomViewPluginForTheApp : public ViewPluginInterface
    2. {
    3. Q_OBJECT
    4. Q_INTERFACES( ViewPluginInterface )
    5. ...
    6. };
    To copy to clipboard, switch view to plain text mode 
    And then at some point in the main application
    Qt Code:
    1. QDir pluginDir( QApplication::applicationDirPath() );
    2.  
    3. if ( !pluginDir.cd( "plugins" ) )
    4. return;
    5.  
    6. foreach( QString fileName, pluginDir.entryList( QDir::Files ) )
    7. {
    8. QPluginLoader loader( pluginDir.absoluteFilePath( fileName ) );
    9. if ( ViewPluginInterface *interface = qobject_cast<ViewPluginInterface*>( loader.instance() ) )
    10. {
    11. // this should be a ViewPluginInterface instance, which in turn is a QWidget instance
    12. ...
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Spooky; 25th January 2011 at 14:51.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: creating QWidget Plugins

    Ok, I think my confusion came from the fact, that I thought the plugin must somehow inherit from QObject and can't be just QWidget only
    QWidget is a QObject.

    Qt Code:
    1. class ViewPluginInterface : public QWidget // to ensure, that anyone using this interface, is creating it as a QWidget
    2. {
    3. ...
    4. };
    To copy to clipboard, switch view to plain text mode 
    You can't do that, since then your interface is not an interface any more.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: creating QWidget Plugins

    Darn, thx . Works now as I intended. However, I made it so that I have a getWidget() function defined in the plugin interface, instead of a createWidget() function, which simply returns this in the implementation of the plugin widget.

    Qt Code:
    1. class ViewPluginInterface
    2. {
    3. public:
    4. virtual QWidget* getWidget() = 0;
    5. };
    6.  
    7. Q_DECLARE_INTERFACE( ViewPluginInterface, "..." )
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class SomeViewPlugin : public QWidget, public ViewPluginInterface
    2. {
    3. Q_OBJECT
    4. Q_INTERFACES( ViewPluginInterface )
    5.  
    6. ...
    7. QWidget* getWidget();
    8. ...
    9. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. ...
    2.  
    3. QWidget* SomeViewPlugin::getWidget()
    4. {
    5. return this;
    6. }
    7.  
    8. ...
    9.  
    10. Q_EXPORT_PLUGIN2( someviewplugin, SomeViewPlugin )
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: creating QWidget Plugins

    However, I made it so that I have a getWidget() function defined in the plugin interface, instead of a createWidget() function, which simply returns this in the implementation of the plugin widget.
    What for?
    You always have an instance of your plugin from the plugin loader...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: creating QWidget Plugins

    Quote Originally Posted by high_flyer View Post
    What for?
    You always have an instance of your plugin from the plugin loader...
    Yes, another stupid thing from me .

    However, the way I implemented it now is still not really the way I want it, because there always only exists one instance of a plugin. There should be multiple instances of these 'views' be available in my application (someone else ran into a similar problem here). But then I am back to this version (more or less), where the plugin interface itself does nothing but specify a factory function for a certain type (e.g. a 'ViewWidget').

Similar Threads

  1. Need help on creating plugins
    By MorrisLiang in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2010, 02:47
  2. Creating MySQL and Oracle (OCI) plugins.
    By Blando in forum Newbie
    Replies: 3
    Last Post: 15th May 2010, 08:45
  3. Replies: 0
    Last Post: 2nd May 2010, 02:55
  4. QWidget-derived Application Plugins
    By SnarlCat in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2008, 18:25
  5. Problem creating a QWidget using threads
    By tarod in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2007, 12:45

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.