简体   繁体   English

将小部件添加到Qt Designer

[英]Adding widgets to Qt Designer

I have recently discovered that Qt supports custom widgets, and that there are sites that provide this kind of widgets ( like Qt-Apps ). 我最近发现Qt支持自定义小部件,并且有些网站提供这种小部件(如Qt-Apps )。 If it is relevant, I am interested in getting this widget. 如果它是相关的,我有兴趣获得这个小部件。

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. 我已经下载了它的源代码,我已经提取了它等等。我还没有尝试构建它,但我感兴趣的是在Qt Designer左侧的widget列表中有这个小部件,所以我可以使用它我的应用程序。

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. 请帮我解决这个问题。

There are two ways: 有两种方法:

A. Use promotion A.使用促销

The simplest way. 最简单的方法。 This method direct take the source without building it. 此方法直接获取源而不构建它。

B. Build a plugin lib for Designer B. 为Designer构建一个插件库

It's a little bit tedious... 这有点单调乏味......


To make it short, supposed you have a desired class(widget): CustomWidget with customwidget.cpp and customwidget.h . 为了CustomWidget它,假设你有一个所需的类(widget): CustomWidget with customwidget.cppcustomwidget.h

  1. Create a new factory class, maybe call it CustomWidgetPlugin and public inherit QObject and QDesignerCustomWidgetInterface and reimplement some virtual functions. 创建一个新的工厂类,可以将其CustomWidgetPlugin并公开继承QObjectQDesignerCustomWidgetInterface并重新实现一些虚函数。

example: 例:

customwidget.h : 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.cpp

constructor: 构造函数:

CustomWidget::CustomWidgetPlugin(QObject *parent)
: QObject(parent)
{
}

name getter: 名字吸气剂:

QString CustomWidgetPlugin::name() const
{
    return "CustomWidget";
}

headerfile getter: headerfile getter:

QString CustomWidgetPlugin::includeFile() const
{
    return "customwidget.h";
}

group name getter: 组名吸气剂:

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 (for the displayed icon in designer): 图标(用于设计器中显示的图标):

QIcon CustomWidgetPlugin::icon() const
{
    return QIcon(":/images/icon.png");
}

tool tip for the widget: 小部件的工具提示:

QString  CustomWidgetPlugin::toolTip() const
{
    return tr("This is a widget, got it?");
}

information of what is this : 这是什么信息:

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;
}

The factory memeber function: 工厂memeber功能:

QWidget *CustomWidgetPlugin::createWidget(QWidget *parent)
{
    return new CustomWidget(parent);
}

IMPORTANT!! 重要!!

At the end of customwidget.cpp file, add this macro: customwidget.cpp文件的末尾,添加以下宏:

Q_EXPORT_PLUGIN2(customwidgetplugin , CustomWidgetPlugin) // (the widget name, the class name)

It makes the plugin available the Qt deisgner. 它使插件可用于Qt deisgner。

Finally, in your .pro file: 最后,在.pro文件中:

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.

After building this project, next time as you open Qt designer, you will see the widget. 构建此项目后,下次打开Qt设计器时,您将看到该小部件。

Ref: C++ GUI Programming with Qt 4 参考: 使用Qt 4进行C ++ GUI编程

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

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