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

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

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

Qt创建设计师自定义控件(Qt Designer自定义控件)_第1张图片

 然后确定,控件类随便添加一个即可。Qt创建设计师自定义控件(Qt Designer自定义控件)_第2张图片

创建好后如下图所示:
Qt创建设计师自定义控件(Qt Designer自定义控件)_第3张图片

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

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

Qt创建设计师自定义控件(Qt Designer自定义控件)_第4张图片

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

#ifndef LQCFCJXPLUGIN_H
#define LQCFCJXPLUGIN_H

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

#include 

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 customWidgets() const;

private:
    QList 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 

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

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 =
        "\n"
        " \n"
        "  \n"
        "   0\n"
        "   0\n"
        "   100\n"
        "   30\n"
        "  \n"
        " \n"
        "\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文件。

Qt创建设计师自定义控件(Qt Designer自定义控件)_第5张图片

         然后将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中使用了。

你可能感兴趣的:(Qt,qt,dll,ui)