#ifndef QLED_H
#定义QLED_H
#包括<Qt>
#include <QWidget>
#include <QtDesigner/QDesignerExportWidget>
QColor 类;
QSvgRenderer 类;
类 QDESIGNER_WIDGET_EXPORT QLed :公共 QWidget
{
Q_OBJECT
Q_ENUMS (ledColor)
Q_ENUMS(ledShape)
Q_PROPERTY(bool value READ value WRITE setValue);
Q_PROPERTY(ledColor onColor READ onColor WRITE setOnColor);
Q_PROPERTY(ledColor offColor READ offColor WRITE setOffColor);
Q_PROPERTY(ledShape 形状 读取形状 写入 setShape)
大众:
QLed(QWidget *parent = 0);
虚拟 ~QLed();
bool value() const { 返回 m_value; }
enum ledColor { 红=0,绿,黄,灰,橙,紫,蓝 };
enum ledShape { Circle=0,Square,Triangle,Rounded};
ledColor onColor() const { 返回 m_onColor; }
ledColor offColor() const { 返回 m_offColor; }
ledShape shape() const { 返回 m_shape; }
公共时段:
void setValue(bool);
void setOnColor(ledColor);
void setOffColor(ledColor);
void setShape(ledShape);
无效切换值();
保护:
bool m_value;
ledColor m_onColor, m_offColor;
int id_Timer;
ledShape m_shape;
QStringList形状;
QStringList颜色;
void PaintEvent(QPaintEvent *event);
私人:
QSvgRenderer *渲染器;
};
#万一
qled.cpp
#包括
#包括
#包括
#包括
#包括
#包括
#include“qled.h”
QLed::QLed(QWidget *父级)
: QWidget(父)
{
m_value=false;
m_onColor=红色;
m_offColor=灰色;
m_shape=圆形;
形状 << ":/resources/circle_" << ":/resources/square_" << ":/resources/triang_" << ":/resources/round_";
颜色<<“红色.svg”<<“绿色.svg”<<“黄色.svg”<<“灰色.svg”<<“橙色.svg”<<“紫色.svg”<<“蓝色.svg” ;
渲染器 = new QSvgRenderer();
}
QLed::~QLed() {
删除渲染器;
}
void QLed::paintEvent(QPaintEvent *)
{
QString ledShapeAndColor;
QPainter画家(本);
Painter.setRenderHint(QPainter::Antialiasing, true);ledShapeAndColor=形状[m_shape];
if(m_value)
ledShapeAndColor.append(colors[m_onColor]);
否则
ledShapeAndColor.append(颜色[m_offColor]);
渲染器->加载(ledShapeAndColor);
渲染器->渲染(&painter);
}
void QLed::setOnColor(ledColor newColor)
{
m_onColor=newColor;
更新();
}
void QLed::setOffColor(ledColor newColor)
{
m_offColor=newColor;
更新();
}
void QLed::setShape(ledShape newShape)
{
m_shape=newShape;
更新();
}
void QLed::setValue(布尔值)
{
m_value=值;
更新();
}
void QLed::toggleValue()
{
m_value=!m_value;
更新();
}
qledplugin.h
#ifndef CUSTOMWIDGETPLUGIN_H
#定义 CUSTOMWIDGETPLUGIN_H
#include <QDesignerCustomWidgetInterface>
类 QLedPlugin :公共 QObject、公共 QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface" FILE "QLedPlugin.json")
Q_INTERFACES(QDesignerCustomWidgetInterface)
大众:
QLedPlugin(QObject *parent = 0);
bool isContainer() const;
bool isInitialized() const;
QIcon icon() const;
QString domXml() const;
QString group() const;
QString includeFile() const;
QString name() const;
QString toolTip() const;
QString WhatsThis() const;
QWidget *createWidget(QWidget *parent);
无效初始化(QDesignerFormEditorInterface *核心);
私人:
bool 初始化;
};
#endif
qledplugin.cpp
#include“qled.h”
#include“qledplugin.h”
#include <QtPlugin>
QLedPlugin::QLedPlugin(QObject *parent)
: QObject(父)
{
初始化= false;
}
void QLedPlugin::initialize(QDesignerFormEditorInterface * )
{
if(已初始化)
返回;
初始化= true;
}
bool QLedPlugin::isInitialized() const
{
返回初始化;
}
QWidget *QLedPlugin::createWidget(QWidget *parent)
{
返回新的QLed(父级);
}
QString QLedPlugin::name() const
{
返回“QLed”;
}
QString QLedPlugin::group() const
{
return "Led Widgets";
}
QIcon QLedPlugin::icon() const
{
return QIcon(":resources/qled.png");
}
QString QLedPlugin::toolTip() const
{
return tr("Qt Designer 的 LED 自定义小部件插件");
}
QString QLedPlugin::whatsThis() const
{
return tr("Qt Designer 的 LED 自定义小部件插件");
}
bool QLedPlugin::isContainer() const
{
返回 false;
}
QString QLedPlugin::domXml() const
{
返回“\n”
“\n”
“\n”
" 0\n"
" 0\n"
“ 50\n”
“ 50\n”
“\n”
“\n”
“\n”
" 二进制 LED\n"
“\n”
“\n”
“假\n”
“\n”
“\n”
“ LED 小部件\n”
“\n”
“\n”
" QLed::红色\n"
“\n”
“\n”
" QLed::灰色\n"
“\n”
“\n”
" QLed::圆\n"
“\n”
"\n";
}
QString QLedPlugin::includeFile() const
{
return "qled.h";
}
我相信到这里所有的东西都已经很清楚了,记得不要忘记QLedPlugin.json喔文件,当然最后也不能少资源文件:
经过编译,后会生成一个qledplugin.lib和qledplugin.dll,
2.配置插件 qt安装目录以D:\Qt\Qt5.1.1\5.1.1\msvc2010为例:
(1)将编译好的qledplugin.dll复制到D:\Qt\Qt5.1.1\5.1.1\msvc2010\bin目录下,
将 qledplugin.lib 复制到 D:\Qt\Qt5.1.1\5.1.1\msvc2010\lib
(2) 将 qledplugin.dll 复制到 D:\Qt\Qt5.1.1\5.1.1\msvc2010\plugins\designer 目录下。
(3) 将qled.h和qled.cpp放入空文件夹QLed中,然后将该文件夹复制到D:\Qt\Qt5.1.1\5.1.1\msvc2010\include目录中。
3。使用插件
创建一个新项目,然后进行配置。主要看.pro怎么写,如下:
QT += 核心 gui svg
大于(QT_MAJOR_VERSION, 4): QT += 小部件
目标 = TestCustomWidgetPlugin
模板=应用程序
INCLUDEPATH += $$(QTDIR)/include/QCustomWidgetPlugin
INCLUDEPATH += $$(QTDIR)/include/QLed
LIBS += $$(QTDIR)/lib/customwidgetplugin.lib \
$$(QTDIR)/lib/qledplugin.lib
源+= main.cpp\
小部件.cpp
标头 += widget.h
表格 += widget.ui
这样所有的任务就完成了,你可以轻松地在设计器中进行设计和预览了。
效果如下:
注:
技术在于沟通。转载时请注明出处,保持作品完整性。
Author: Yiqu丶二三里 Original text: http://www.gsm-guard.net/s/blog_a6fb6cc90102vsj1.html.
-->