简体   繁体   中英

How to create a qt plugin that inherits from QWidget

I was just reading up about QWidgets and I would like to create plugins that are widgets which will be loaded during runtime. When I was looking at the sample code and the requirements, the plugin seems to inherit from the interface and QObject. How would I create QWidget-plugins where they all have different buttons and different slots? Also, would it be possible to create a plugin that inherits from the interface and a baseclass that inherits from QWidget(that inherits from QObject).

http://developer.qt.nokia.com/doc/qt-4.8/plugins-howto.html

However, I read about metaobject where you can load widgets during runtime, just by knowing their names (doesn't require RTTI support). But exactly how would I delived the classes to the porject for it to recognize them? Whn dealing with plugins I need to have them in a special project that I compile with a different sets of flags in the .pro file. But how would I do it here?

I would really like to use the qtplugin but how?

An Idea:

Would it be acceptable and optimal to let the plugin create a QWidget that it returns? Don't really see the point of writing plugins to the designer if I create my interface without it. Or have I misunderstood it?

http://techbase.kde.org/Development/Tutorials/Writing_Qt_Designer_Plugins

class workspaceInterface {
   virtual QWidget* createWorkspace(QWidget* parent);
   ... other useful functions...
}

class mySpecialWidget : public QWidget {
    mySpecialWidget {
        add a layout, add some buttons, maybe change the color
    }
}

//plugin
#include "myspecialwidget.h"

class myPlugin : public QObject, public workspaceInterface {
    QWidget* createWorkspace(QWidget* parent) {
        return new MySpecialWidget();
    }
    .... 
}

All of this code I would put in one project, compile it as a plugin and then look for it in my main application and load it in. The I would create a instance of it and let it create a widget that I will display.

Is there any better way of doing this, or is this it?

You may be looking for Creating Custom Widgets for Qt Designer . Widget plug-ins do inherit from QWidget . For example, see World Time Clock Plugin .

While widget plug-ins are intended mostly for Qt Designer, some of their functionality can be used during runtime with the help of QUiLoader .

Edit:

It is definitely some investment to write Qt Designer plug-ins. If you do not need design time support, you may find little value in creating them.

For your own plug-in infrastructure you can package plug-ins in a set of dynamic libraries with standardized API (export functions) to invoke them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM