Results 1 to 8 of 8

Thread: custom widgets as plugins

  1. #1
    Join Date
    Jan 2006
    Posts
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default custom widgets as plugins

    Hi all,

    What is the correct strategy for creating a graphical component as a plugin?

    For example, if I have an application with two components in the main window, (a browse tree on the left, and a viewing pane on the right), I want to implement each component as a plugin, so that I can drop in new components withou re-compiling my main app.

    So I get the view plugin:

    Qt Code:
    1. ViewInterface *iView = qobject_cast<ViewInterface *>(plugin);
    To copy to clipboard, switch view to plain text mode 

    Then get an instance of the view widget:

    Qt Code:
    1. if (iView){
    2. viewWidget = iView->createWidget(this);
    3. }
    To copy to clipboard, switch view to plain text mode 

    where viewWidget is a QWidget*

    So now I can insert the plugin into my layout, but I can't access any of the plugins methods because it's now a QWidget, not a Viewer!

    I guess this question is more conceptual than technical... any advice?

    -Chris

  2. #2
    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: custom widgets as plugins

    Create a common base for all plugins (or separate for each plugin type) and implement such an interface in your plugins. You'll only be able to call methods defined in that base, but that's obvious.

    Read the docs and/or search the forum for details. Especially take a look at the "plug and paint" example bundled with Qt.

  3. #3
    Join Date
    Jan 2006
    Posts
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom widgets as plugins

    Hiya,

    I've looked at that example alright, but it doesn't create a gui component, the plugin just provides functionality. What I'm trying to do is load a plugin (or plugins) and insert them into a layout. To do this they will have to be sublassed from a QWidget, but also conform to the plugin interface...

    Thanks.
    Chris

  4. #4
    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: custom widgets as plugins

    So? What is the problem?
    Qt Code:
    1. struct WidgetInterface{
    2. virtual QWidget *create(QWidget *parent)=0;
    3. // ...
    4. }
    5. struct ListViewInterface{
    6. virtual QListView *create(QWidget *parent)=0;
    7. // ...
    8. }
    To copy to clipboard, switch view to plain text mode 

    Or even use only WidgetInterface and check the widgets classname using Qt's meta data access and cast it to its proper class.

  5. #5
    Join Date
    Jan 2006
    Posts
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom widgets as plugins

    The problem with this:
    Qt Code:
    1. struct ListViewInterface{
    2. virtual MyListViewWidget *create(QWidget *parent)=0;
    3. ...
    4. };
    To copy to clipboard, switch view to plain text mode 
    is that the application has to know what a MyListViewWidget class looks like. Which defeats the purpose of the interface.

    What is needed is something like:
    Qt Code:
    1. struct ListViewInterface{
    2. virtual QWidget *create(QWidget *parent)=0;
    3. ...
    4. };
    To copy to clipboard, switch view to plain text mode 
    where I could then create a set of plugins:
    MyListView
    YourListView
    AnotherListView
    etc...
    that all adhere to the ListViewInterface interface.

    With the interface defined above, I can call create() and get back a QWidget and then stick that in the layout, but I can't call any of the ListViewInterface defined methods anymore , because i have a QWidget now, not a (QWidget+ListViewInterface)

    So, as per your second suggestion, I guess I need to use Qt's meta data access and cast it to its proper class.

    Thanks.
    -C.

  6. #6
    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: custom widgets as plugins

    But you don't have to return a QWidget from the create method. You can return MyListViewWidget, provided that your main application knows that class.

  7. #7
    Join Date
    Jan 2006
    Posts
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom widgets as plugins

    I know , but as I said in my previous append, this defeats the purpose of the interface, because you have to tell the main the specifics of the class instead of the generics of the interface.

    What is needed is a main that uses the interface to communicate with a set of plugins that obey the interface. However the plugins also have to be Widgets so that main can stick them in a layout.

    Hmmm... is this possible:

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

    ????

    probably not... must try it... back in a sec!
    Chris

  8. #8
    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: custom widgets as plugins

    The interface shouldn't inherit QWidget, because the interface is the interface of your plugin, whereas the plugin (implementing the interface) can create widgets. Now the point is to define base classes for those widgets, so that you can communicate with your plugin-originated widgets without knowing their exact structures. So you can have something like this:

    Qt Code:
    1. struct MyIFace {
    2. QStringList treeviews()=0;
    3. QStringList simplewidgets()=0;
    4. QStringList buttons()=0;
    5. QStringList gizmos()=0;
    6. QAbstractButton *createButton(const QString &classname, QWidget *p=0)=0;
    7. QWidget *createWidget(const QString &classname, QWidget *p=0)=0;
    8. QTreeView *createTreeView(const QString &classname, QWidget *p=0)=0;
    9. Gizmo *createGizmo(const QString &classname, QWidget *p=0)=0;
    10. };
    To copy to clipboard, switch view to plain text mode 
    with Gizmo being your totally custom widget with custom interface.
    Members returning string lists let you know, what classes the plugin is able to generate. Knowing that you can call one of "create" methods passing one of the names retrieved earlier and you receive a widget compliant with the given interface. The "Gizmo" thing shows that you're not limited to standard bases, you can have your own ones.

Similar Threads

  1. Custom Widgets and layout managers...
    By TemporalBeing in forum Qt Programming
    Replies: 6
    Last Post: 4th March 2009, 14:48
  2. Replies: 4
    Last Post: 2nd December 2008, 05:19
  3. Replies: 2
    Last Post: 16th May 2008, 15:39
  4. picking geometry in qt custom widgets
    By notsonerdysunny in forum Qt Programming
    Replies: 2
    Last Post: 18th July 2007, 00:01
  5. create custom widgets
    By nimmyj in forum General Discussion
    Replies: 1
    Last Post: 20th November 2006, 09:24

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.