将小部件添加到 Qt 设计器 [英] Adding widgets to Qt Designer
问题描述
我最近发现 Qt 支持自定义小部件,并且有些网站提供这种小部件(例如 Qt-Apps).如果相关,我有兴趣获得 this小部件.
I have recently discovered that Qt supports custom widgets, and that there are sites that provide this kind of widgets ( like Qt-Apps ). If it is relevant, I am interested in getting this widget.
我已经下载了它的源代码,我已经提取了它等等.我还没有尝试构建它,但我感兴趣的是在 Qt 设计器左侧的小部件列表中拥有该小部件以便我可以在我的应用程序中使用它.
I have downloaded the source code for it, I have extracted it etc. I haven't tried to build it yet, but what I am interested in is having that widget in the widget list in the left side of the Qt Designer so that I can use it my applications.
我要么不知道如何搜索我要查找的内容,要么根本不存在.请帮我解决这个问题.
I either don't know how to search what I am looking for, or it doesn't exist at all. Please help me with this problem.
推荐答案
有两种方式:
A.使用促销
最简单的方法.此方法直接取源码,无需编译.
The simplest way. This method direct take the source without building it.
有点乏味...
简而言之,假设您有一个所需的类(小部件):CustomWidget
和 customwidget.cpp
和 customwidget.h
.
To make it short, supposed you have a desired class(widget): CustomWidget
with customwidget.cpp
and customwidget.h
.
- 创建一个新的工厂类,可以调用它
CustomWidgetPlugin
并公开继承QObject
和QDesignerCustomWidgetInterface
并重新实现一些虚函数.
- Create a new factory class, maybe call it
CustomWidgetPlugin
and public inheritQObject
andQDesignerCustomWidgetInterface
and reimplement some virtual functions.
示例:
customwidget.h
:
#include <QDesignerCustomWidgetInterface>
#include "customwidget.h"
class CustomWidgetPlugin : public QObject, public QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_INTERFACES(QDesignerCustomWidgetInterface) // note this line, it tell moc that the second base class is a plugin interface.
public:
CustomWidget(QObject *parent = 0);
QString name() const;
QString includeFile() const;
QString group() const;
QIcon icon() const;
QString toolTip() const;
QString whatsThis() const;
bool isContainer() const;
QWidget *createWidget(QWidget *parent);
};
customwidget.cpp
:
构造函数:
CustomWidget::CustomWidgetPlugin(QObject *parent)
: QObject(parent)
{
}
名称获取器:
QString CustomWidgetPlugin::name() const
{
return "CustomWidget";
}
头文件获取器:
QString CustomWidgetPlugin::includeFile() const
{
return "customwidget.h";
}
组名获取器:
QString CustomWidgetPlugin::group() const
{
return tr("New Group");
}
(组名定义小部件所属的位置,如果名称不适合任何默认组,它将创建一个新组)
(the group name define where the widget belongs, and it creates a new group if the name doesn't fit any default group)
icon(用于设计器中显示的图标):
icon (for the displayed icon in designer):
QIcon CustomWidgetPlugin::icon() const
{
return QIcon(":/images/icon.png");
}
小部件的工具提示:
QString CustomWidgetPlugin::toolTip() const
{
return tr("This is a widget, got it?");
}
这是什么的信息:
QString CustomWidgetPlugin::whatsThis() const
{
return tr("A widget, already said.");
}
定义它是否是一个容器"(是否可以容纳另一个小部件):
define if it is a "container" (can hold another widget or not):
bool CustomWidgetPlugin::isContainer() const
{
return false;
}
工厂成员函数:
QWidget *CustomWidgetPlugin::createWidget(QWidget *parent)
{
return new CustomWidget(parent);
}
重要!!
在customwidget.cpp
文件的末尾,添加这个宏:
IMPORTANT!!
At the end of customwidget.cpp
file, add this macro:
Q_EXPORT_PLUGIN2(customwidgetplugin , CustomWidgetPlugin) // (the widget name, the class name)
它使插件可用于 Qt 设计器.
It makes the plugin available the Qt deisgner.
最后,在您的 .pro
文件中:
Finally, in your .pro
file:
TEMPLATE = lib
CONFIG += designer plugin release
HEADERS = ../customwidget.h \
customwidgetplugin.h
SOURCES = ../customwidget.cpp \
customwidgetplugin.cpp
RESOURCES = customwidget.qrc
DESTDIR = $(QTDIR)/plugins/designer #assume QTDIR environment variable is set to the directory where Qt is installed.
构建此项目后,下次打开 Qt 设计器时,您将看到小部件.
After building this project, next time as you open Qt designer, you will see the widget.
参考:使用 Qt 4 进行 C++ GUI 编程
这篇关于将小部件添加到 Qt 设计器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!