如何创建一个继承于QWidget的qt插件
我刚刚阅读了有关 QWidgets 的内容,我想创建一些插件,这些插件是在运行时加载的小部件。当我查看示例代码和需求时,该插件似乎继承自接口和 QObject。我将如何创建 QWidget 插件,它们都有不同的按钮和不同的插槽?另外,是否可以创建一个继承自接口的插件和一个继承自 QWidget(继承自 QObject)的基类。
http://developer.qt.nokia.com/doc/qt -4.8/plugins-howto.html
但是,我读到了有关元对象的信息,您可以在运行时加载小部件,只需知道它们的名称(不需要 RTTI 支持)。但我到底如何将课程传递给项目以使其识别它们呢?在处理插件时,我需要将它们放在一个特殊的项目中,我使用 .pro 文件中的不同标志集进行编译。但我在这里该怎么做呢?
我真的很想使用 qtplugin 但如何呢?
一个想法:
让插件创建它返回的 QWidget 是否可以接受且最佳?如果我在没有插件的情况下创建界面,那么我真的不明白为设计器编写插件的意义。还是我理解错了?
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();
}
....
}
所有这些代码我会放入一个项目,将其编译为插件,然后在我的主应用程序中查找它并加载它。我会创建它的一个实例,然后让它创建一个我将显示的小部件。
有没有更好的方法可以做到这一点,或者就是这样?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能正在寻找 为 Qt Designer 创建自定义小部件。 Widget 插件继承自
QWidget
。例如,请参阅世界时钟插件。虽然小部件插件主要用于 Qt Designer,但它们的某些功能可以在运行时在 QUiLoader.
编辑:
编写 Qt Designer 插件绝对是一些投资。如果您不需要设计时支持,您可能会发现创建它们没有什么价值。
对于您自己的插件基础设施,您可以将插件打包在一组动态库中,并使用标准化 API(导出函数)来调用它们。
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.