且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将小部件添加到 Qt 设计器

更新时间:2023-11-14 17:12:28

有两种方式:

A.使用促销

最简单的方法.此方法直接取源码,无需编译.

The simplest way. This method direct take the source without building it.

B.为 Designer 构建插件库

有点乏味...

简而言之,假设您有一个所需的类(小部件):CustomWidgetcustomwidget.cppcustomwidget.h.

To make it short, supposed you have a desired class(widget): CustomWidget with customwidget.cpp and customwidget.h.

  1. 创建一个新的工厂类,可以调用它CustomWidgetPlugin 并公开继承QObjectQDesignerCustomWidgetInterface 并重新实现一些虚函数.
  1. Create a new factory class, maybe call it CustomWidgetPlugin and public inherit QObject and QDesignerCustomWidgetInterface 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 编程