简体   繁体   English

如何创建从QWidget继承的Qt插件

[英]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. 我只是在阅读有关QWidgets的文章,我想创建作为插件的插件,这些插件将在运行时加载。 When I was looking at the sample code and the requirements, the plugin seems to inherit from the interface and QObject. 当我查看示例代码和要求时,该插件似乎继承自接口和QObject。 How would I create QWidget-plugins where they all have different buttons and different slots? 如何创建QWidget插件,它们都具有不同的按钮和不同的插槽? 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). 同样,有可能创建一个从接口继承的插件和一个从QWidget(从QObject继承)继承的基类。

http://developer.qt.nokia.com/doc/qt-4.8/plugins-howto.html 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). 但是,我了解了有关metaobject的信息,您只需知道它们的名称即可在运行时在其中加载小部件(不需要RTTI支持)。 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. 处理插件时,我需要将它们放在一个特殊的项目中,该项目使用.pro文件中的不同标志集进行编译。 But how would I do it here? 但是我在这里怎么办?

I would really like to use the qtplugin but how? 我真的很想使用qtplugin,但是如何使用?

An Idea: 一个主意:

Would it be acceptable and optimal to let the plugin create a QWidget that it returns? 让该插件创建一个返回的QWidget是否可以接受并且是最佳选择? 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 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 . 您可能正在寻找为Qt Designer创建自定义窗口小部件 Widget plug-ins do inherit from QWidget . Widget插件确实继承自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 . 尽管窗口小部件插件主要用于Qt Designer,但它们的某些功能可以在运行时借助QUiLoader来使用。

Edit: 编辑:

It is definitely some investment to write Qt Designer plug-ins. 编写Qt Designer插件绝对是一笔投资。 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. 对于您自己的插件基础结构,您可以将插件打包在带有标准API(导出函数)的一组动态库中,以调用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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