文章目录
Qt自定义控件插件类
plugin.h
#ifndef PLUGIN_H
#define PLUGIN_H
#include <QtUiPlugin/QDesignerCustomWidgetInterface>
/***************************************************************
* @brief 自定义控件插件类模板
* ElementName 自定义控件类名
* bIsContainer 是否容器(若是容器则必须置于一个窗口中,主要用于自定义布局,一般控件填写false即可)
* strIconPath 图标路径(用于在QtDesigner的控件列表中显示,无特殊要求填写空串即可)
* strGroup 分组名称(用于在QtDesigner的控件列表中分组显示,为空时会显示在末尾)
* strDomXml 初始ui(用于生成初始ui文件,使用DOMXML相关宏生成即可,填写格式参考ui文件[使用普通文本格式打开])
**************************************************************/
#define DECLARE_CUSTOM_ELEMENT_PLUGIN(ElementName, bIsContainer, strIconPath, strGroup, strDomXml) \
class ElementName##Plugin : public QObject, public QDesignerCustomWidgetInterface {\
Q_OBJECT\
Q_INTERFACES(QDesignerCustomWidgetInterface)\
public:\
ElementName##Plugin(QObject *parent = nullptr) : QObject(parent) { m_initialized = false; }\
bool isInitialized() const { return m_initialized; }\
QWidget *createWidget(QWidget *parent) { \
return new ElementName(parent); \
}\
QString name() const { return #ElementName; }\
QString group() const { return strGroup; }\
QIcon icon() const { return QIcon(strIconPath); }\
QString toolTip() const { return #ElementName; }\
QString whatsThis() const { return #ElementName; }\
bool isContainer() const override { return bIsContainer; }\
QString domXml() const { return strDomXml; }\
QString includeFile() const { return #ElementName".h"; }\
private:\
bool m_initialized;\
};
/***************************************************************
* @brief 初始ui生成模板
* ElementName 自定义控件类名
* ObjectName 对象的默认名称
* Width 控件默认宽度
* Height 控件默认高度
**************************************************************/
#define DOMXML_DEFAULT(ElementName, ObjectName) QString::fromUtf8("<widget class=\"%1\" name=\"%2\"/>").arg(#ElementName).arg(#ObjectName);
#define DOMXML_WITH_DEFAULTSIZE(ElementName, ObjectName, Width, Height) "\
<widget class=\""#ElementName"\" name=\""#ObjectName"\">\n\
<property name=\"geometry\">\n\
<rect>\n\
<width>"#Width"</width>\n\
<height>"#Height"</height>\n\
</rect>\n\
</property>\n\
</widget>\n"
/***************************************************************
* @brief 再此添加需要注册的自定义控件
* 1、Q_OBJECT必须定义在头文件
* 2、Q_OBJECT与类声明必须在同一个文件中
* 没有找到解决办法,暂时放在这里
**************************************************************/
#include "MyWidget.h"
DECLARE_CUSTOM_ELEMENT_PLUGIN(MyWidget, true, "", "CustomStyle", DOMXML_WITH_DEFAULTSIZE(MyWidget, wgt, 120, 80))
#endif //PLUGIN_H
CustomStyle.h
#ifndef CUSTOMSTYLE_H
#define CUSTOMSTYLE_H
#include <QtUiPlugin/QDesignerCustomWidgetInterface>
/***************************************************************
* @brief CustomStyle 自定义控件库插件类,用于注册QtDesigner自定义控件
**************************************************************/
class CustomStyle : public QObject, public QDesignerCustomWidgetCollectionInterface
{
Q_OBJECT
Q_INTERFACES(QDesignerCustomWidgetCollectionInterface)
#if QT_VERSION >= 0x050000
Q_PLUGIN_METADATA(IID "CustomStyle")
#endif // QT_VERSION >= 0x050000
public:
explicit CustomStyle(QObject *parent = 0);
virtual QList<QDesignerCustomWidgetInterface*> customWidgets() const;
private:
QList<QDesignerCustomWidgetInterface*> m_widgets;
};
#endif // CUSTOMSTYLE_H
CustomStyle.cpp
#include "customstyle.h"
#include "Plugin.h"
/***************************************************************
* @brief 用于将控件插件添加到自定义控件列表
* ElementName 自定义控件类名
**************************************************************/
#define REGISTER_CUSTOM_ELEMENT(ElementName) m_widgets.append(new ElementName##Plugin(this));
CustomStyle::CustomStyle(QObject *parent)
: QObject(parent)
{
//将需要注册的自定义控件添加到控件列表
//TODO:在此添加需要注册到QtDesigner的自定义控件
REGISTER_CUSTOM_ELEMENT(MyWidget);
}
DesignerCustomWidgetInterface*> CustomStyle::customWidgets() const
{
return m_widgets;
}