Results 1 to 16 of 16

Thread: creating QWidget Plugins

  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 15: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').

  12. #12
    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

    There should be multiple instances of these 'views' be available in my application
    It looks to me you are confusing things.
    And I start to suspect you don't really need a plugin.
    Can you explain why it is you are using a plugin and not a normal DLL/shared obj?
    You can expose and any nuber of interfaces with your plugin - you only need some sort of function for allocating various types and asking which type your object is, so that you can do the correct conversion on the application side.
    So one interface will deal with factoring, the other will describe the various types.
    But again the question is WHY do you need it?
    Plugins are needed, when you want to be able to extend your application with unknown functionality, that conforms to some rules (interface).
    Like new filters to an image editing program for example.
    Is this you case?
    I suspect not, but I don't know until you explain some more on what it is you are trying to do.
    ==========================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.

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

    Default Re: creating QWidget Plugins

    It's an application to visualize data. This application will consist of different views to display the data. The application should be able to be extensible with additional views (each view can have multiple instances), that access the same data, via plugins (dlls).

    I was simply trying to implement this in a simple way with the QPluginLoader. I thought that this might be a straight forward and easily implementable approach to this problem.

  14. #14
    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

    The application should be able to be extensible with additional views (each view can have multiple instances), that access the same data, via plugins (dlls).
    That means, you don't need the views supplied from the plugin, but only the extra functionality, exactly as I said in my previous post.
    And each plugin will implement one sort of functionality. (for example a filter).
    So you don't have to worry about all the stuff you were worrying about.
    You application can spawn new views for each found plugin.

    Start by implementing one plugin, then you will understand.
    Have a look at the examples in the Qt documentation.
    ==========================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.

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

    Default Re: creating QWidget Plugins

    Hm, well I did look at the examples and I did implement one 'dummy' view as a plugin, via the method I described before. The examples extend, as you said, just certain functionality, but that is not what I need.

    The functionality of each view can be very different, just the interface with the main application is always the same. To provide a more specific example, there could be a Scatter Plot view in the application and a Parallel Coordinates view. Both views access the same data, but both views are controlled differently, display different parts of the data and handle the received data differently.

    What I want to do is creating plugins that simply implement a certain interface, are loaded into the main application as Widgets and are otherwise independent from each other. The user interface, the visual representation etc. can all be completely different for each view, it's up to the implementation of each individual view.

  16. #16
    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

    To provide a more specific example, there could be a Scatter Plot view in the application and a Parallel Coordinates view. Both views access the same data, but both views are controlled differently, display different parts of the data and handle the received data differently.
    Then you do the "data crunching" in your plugin, and your application takes the results from the plugin and stores it in a model.
    You can then on the applicaiton level spawn as many views as you wish and feed them your model, and you can show in each view any segement of the data in any way you choose, in parallel.
    You are over complicating things!
    ==========================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.

Similar Threads

  1. Need help on creating plugins
    By MorrisLiang in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2010, 03:47
  2. Creating MySQL and Oracle (OCI) plugins.
    By Blando in forum Newbie
    Replies: 3
    Last Post: 15th May 2010, 09:45
  3. Replies: 0
    Last Post: 2nd May 2010, 03:55
  4. QWidget-derived Application Plugins
    By SnarlCat in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2008, 19:25
  5. Problem creating a QWidget using threads
    By tarod in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2007, 13: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.