Qt创建设计师自定义控件(Qt Designer自定义控件)

        Qt提供了一个Designer界面,使得我们可以自由拖动控件对ui进行布局,但所有的控件都是固定的,如果我们想要自定义一个自己设计的控件,添加到Designer中,可供设计者自由设计。那么可以进行如下操作。

首先新建一个项目,选择其他项目,点击Qt4设计师自定义控件。

 然后确定,控件类随便添加一个即可。

创建好后如下图所示:

 为了扩展多个控件,我们将lqcfcjx.h和lqcfcjx.cpp文件删除,在其他项目中单独设计,然后并在lqcfcjx.pri中扩展其他项目,Switch是一个单独的项目如下:

//lqcfcjx.pri
include(Switch/Switch.pri)

 对于lqcfcjxplugin.h中创建一个自定义控件接口,如下:
 

#ifndef LQCFCJXPLUGIN_H
#define LQCFCJXPLUGIN_H

//插件子项目的头文件
#include"switch.h"

#include <QDesignerCustomWidgetInterface>

class CustomWidgetInterface: public QObject,
                              public QDesignerCustomWidgetInterface
{
    Q_OBJECT
    Q_INTERFACES( QDesignerCustomWidgetInterface )

public:
    CustomWidgetInterface( QObject *parent );

    virtual bool isContainer() const;
    virtual bool isInitialized() const;
    virtual QIcon icon() const;
    virtual QString codeTemplate() const;
    virtual QString domXml() const;
    virtual QString group() const;
    virtual QString includeFile() const;
    virtual QString name() const;
    virtual QString toolTip() const;
    virtual QString whatsThis() const;
    virtual void initialize( QDesignerFormEditorInterface * );

protected:
    bool d_isContainer{false};
    QString d_name;
    QString d_include;
    QString d_toolTip;
    QString d_whatsThis;
    QString d_domXml;
    QString d_codeTemplate;
    QString d_goup = "ysyyrps";
    QIcon d_icon;

private:
    bool d_isInitialized;
};

class CustomWidgetCollectionInterface: public QObject,
                                        public QDesignerCustomWidgetCollectionInterface
{
    Q_OBJECT
    Q_INTERFACES( QDesignerCustomWidgetCollectionInterface )

    #if QT_VERSION >= 0x050000
            Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetCollectionInterface" )
    #endif

public:
    CustomWidgetCollectionInterface( QObject *parent = NULL );

    virtual QList<QDesignerCustomWidgetInterface*> customWidgets() const;

private:
    QList<QDesignerCustomWidgetInterface*> d_plugins;
};

//子项目继承
class SwitchInterface: public CustomWidgetInterface
{
    Q_OBJECT
    Q_INTERFACES( QDesignerCustomWidgetInterface )

public:
    SwitchInterface( QObject *parent );
    virtual QWidget *createWidget( QWidget *parent );
};

#endif // LQCFCJXPLUGIN_H

具体实现为:

#include "lqcfcjxplugin.h"
#include <QtPlugin>

CustomWidgetInterface::CustomWidgetInterface( QObject *parent ):
    QObject( parent ),
    d_isInitialized( false )
{
}

bool CustomWidgetInterface::isContainer() const
{
    return d_isContainer;
}

bool CustomWidgetInterface::isInitialized() const
{
    return d_isInitialized;
}

QIcon CustomWidgetInterface::icon() const
{
    return d_icon;
}

QString CustomWidgetInterface::codeTemplate() const
{
    return d_codeTemplate;
}

QString CustomWidgetInterface::domXml() const
{
    return d_domXml;
}

QString CustomWidgetInterface::group() const
{
    return d_goup;
}

QString CustomWidgetInterface::includeFile() const
{
    return d_include;
}

QString CustomWidgetInterface::name() const
{
    return d_name;
}

QString CustomWidgetInterface::toolTip() const
{
    return d_toolTip;
}

QString CustomWidgetInterface::whatsThis() const
{
    return d_whatsThis;
}

void CustomWidgetInterface::initialize(
    QDesignerFormEditorInterface *)
{
    if ( d_isInitialized )
        return;
    d_isInitialized = true;
}

CustomWidgetCollectionInterface::CustomWidgetCollectionInterface(
    QObject *parent ):
    QObject( parent )
{
    d_plugins.append(new SwitchInterface(this));
}

QList<QDesignerCustomWidgetInterface*>

CustomWidgetCollectionInterface::customWidgets( void ) const
{
    return d_plugins;
}

SwitchInterface::SwitchInterface(QObject *parent): CustomWidgetInterface(parent)
{
    d_name = "Switch";
    d_include = "switch.h";
    d_icon = QPixmap( ":/icons/switch.png" );
    d_isContainer = true;
    d_domXml =
        "<widget class=\"Switch\" name=\"switch\">\n"
        " <property name=\"geometry\">\n"
        "  <rect>\n"
        "   <x>0</x>\n"
        "   <y>0</y>\n"
        "   <width>100</width>\n"
        "   <height>30</height>\n"
        "  </rect>\n"
        " </property>\n"
        "</widget>\n";

}

QWidget *SwitchInterface::createWidget(QWidget *parent)
{
    return new Switch(parent);
}

#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2(lqcfcjx, lqcfcjxPlugin)
#endif // QT_VERSION < 0x050000

        然后点击构建,就会在对应的Debug或Release文件夹下生成对应的dll和lib文件。

         然后将lqcfcjx.dll,lqcfcjxd.dll,lqcfcjx.lib,lqcfcjxd.lib文件放到Qt安装的designer目录下即可。如C:\install\Qt\5.14.2\msvc2017\plugins\designer和C:\install\Qt\Tools\QtCreator\bin\plugins\designer。这个是我的Qt的两个目录,然后即可在QtDesigner中使用了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值