• 2079阅读
  • 0回复

Qt编写自定义控件38-高亮按钮 [复制链接]

上一主题 下一主题
离线liudianwu
 

只看楼主 倒序阅读 楼主  发表于: 2019-07-25

一、前言
高亮按钮控件,既可以作为类似于交通指示灯使用,也可以作为设备状态指示灯使用,控件内置多套颜色风格,还可以自己设置颜色风格,按钮可以增加文字显示,非常适合需要在状态设备上显示小量的文字展示,按钮还可以开启报警,开启后会红黑闪烁,也可以自定义设置报警的两种颜色,除了默认是圆形外,还可以设置成矩形模式,控件写好了作为独立控件拖动使用,这样的话可以将控件作为一个设备,在地图上拖动,用户只需要开启拖动即可,不需要再自己编码。

二、实现的功能
* 1:可设置文本,居中显示
* 2:可设置文本颜色
* 3:可设置外边框渐变颜色
* 4:可设置里边框渐变颜色
* 5:可设置背景色
* 6:可直接调用内置的设置 绿色/红色/黄色/黑色/蓝色 等公有槽函数
* 7:可设置是否在容器中可移动,当成一个对象使用
* 8:可设置是否显示矩形
* 9:可设置报警颜色+非报警颜色
* 10:可控制启动报警和停止报警,报警时闪烁

三、效果图


四、头文件代码
  1. #ifndef LIGHTBUTTON_H
  2. #define LIGHTBUTTON_H
  3. /**
  4. * 高亮发光按钮控件 作者:feiyangqingyun(QQ:517216493) 2016-10-16
  5. * 1:可设置文本,居中显示
  6. * 2:可设置文本颜色
  7. * 3:可设置外边框渐变颜色
  8. * 4:可设置里边框渐变颜色
  9. * 5:可设置背景色
  10. * 6:可直接调用内置的设置 绿色/红色/黄色/黑色/蓝色 等公有槽函数
  11. * 7:可设置是否在容器中可移动,当成一个对象使用
  12. * 8:可设置是否显示矩形
  13. * 9:可设置报警颜色+非报警颜色
  14. * 10:可控制启动报警和停止报警,报警时闪烁
  15. */
  16. #include <QWidget>
  17. #ifdef quc
  18. #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
  19. #include <QtDesigner/QDesignerExportWidget>
  20. #else
  21. #include <QtUiPlugin/QDesignerExportWidget>
  22. #endif
  23. class QDESIGNER_WIDGET_EXPORT LightButton : public QWidget
  24. #else
  25. class LightButton : public QWidget
  26. #endif
  27. {
  28.     Q_OBJECT
  29.     Q_PROPERTY(QString text READ getText WRITE setText)
  30.     Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)
  31.     Q_PROPERTY(QColor alarmColor READ getAlarmColor WRITE setAlarmColor)
  32.     Q_PROPERTY(QColor normalColor READ getNormalColor WRITE setNormalColor)
  33.     Q_PROPERTY(QColor borderOutColorStart READ getBorderOutColorStart WRITE setBorderOutColorStart)
  34.     Q_PROPERTY(QColor borderOutColorEnd READ getBorderOutColorEnd WRITE setBorderOutColorEnd)
  35.     Q_PROPERTY(QColor borderInColorStart READ getBorderInColorStart WRITE setBorderInColorStart)
  36.     Q_PROPERTY(QColor borderInColorEnd READ getBorderInColorEnd WRITE setBorderInColorEnd)
  37.     Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
  38.     Q_PROPERTY(bool canMove READ getCanMove WRITE setCanMove)
  39.     Q_PROPERTY(bool showRect READ getShowRect WRITE setShowRect)
  40.     Q_PROPERTY(bool showOverlay READ getShowOverlay WRITE setShowOverlay)
  41.     Q_PROPERTY(QColor overlayColor READ getOverlayColor WRITE setOverlayColor)
  42. public:
  43.     explicit LightButton(QWidget *parent = 0);
  44. protected:
  45.     bool eventFilter(QObject *watched, QEvent *event);
  46.     void paintEvent(QPaintEvent *);
  47.     void drawBorderOut(QPainter *painter);
  48.     void drawBorderIn(QPainter *painter);
  49.     void drawBg(QPainter *painter);
  50.     void drawText(QPainter *painter);
  51.     void drawOverlay(QPainter *painter);
  52. private:
  53.     QString text;                   //文本
  54.     QColor textColor;               //文字颜色
  55.     QColor alarmColor;              //报警颜色
  56.     QColor normalColor;             //正常颜色
  57.     QColor borderOutColorStart;     //外边框渐变开始颜色
  58.     QColor borderOutColorEnd;       //外边框渐变结束颜色
  59.     QColor borderInColorStart;      //里边框渐变开始颜色
  60.     QColor borderInColorEnd;        //里边框渐变结束颜色
  61.     QColor bgColor;                 //背景颜色
  62.     bool showRect;                  //显示成矩形
  63.     bool canMove;                   //是否能够移动
  64.     bool showOverlay;               //是否显示遮罩层
  65.     QColor overlayColor;            //遮罩层颜色
  66.     QTimer *timerAlarm;             //定时器切换颜色
  67. public:
  68.     QString getText()               const;
  69.     QColor getTextColor()           const;
  70.     QColor getAlarmColor()          const;
  71.     QColor getNormalColor()         const;
  72.     QColor getBorderOutColorStart() const;
  73.     QColor getBorderOutColorEnd()   const;
  74.     QColor getBorderInColorStart()  const;
  75.     QColor getBorderInColorEnd()    const;
  76.     QColor getBgColor()             const;
  77.     bool getCanMove()               const;
  78.     bool getShowRect()              const;
  79.     bool getShowOverlay()           const;
  80.     QColor getOverlayColor()        const;
  81.     QSize sizeHint()                const;
  82.     QSize minimumSizeHint()         const;
  83. public Q_SLOTS:
  84.     //设置文本
  85.     void setText(const QString &text);
  86.     //设置文本颜色
  87.     void setTextColor(const QColor &textColor);
  88.     //设置报警颜色+正常颜色
  89.     void setAlarmColor(const QColor &alarmColor);
  90.     void setNormalColor(const QColor &normalColor);
  91.     //设置外边框渐变颜色
  92.     void setBorderOutColorStart(const QColor &borderOutColorStart);
  93.     void setBorderOutColorEnd(const QColor &borderOutColorEnd);
  94.     //设置里边框渐变颜色
  95.     void setBorderInColorStart(const QColor &borderInColorStart);
  96.     void setBorderInColorEnd(const QColor &borderInColorEnd);
  97.     //设置背景色
  98.     void setBgColor(const QColor &bgColor);
  99.     //设置是否可移动
  100.     void setCanMove(bool canMove);
  101.     //设置是否显示矩形
  102.     void setShowRect(bool showRect);
  103.     //设置是否显示遮罩层
  104.     void setShowOverlay(bool showOverlay);
  105.     //设置遮罩层颜色
  106.     void setOverlayColor(const QColor &overlayColor);
  107.     //设置为绿色
  108.     void setGreen();
  109.     //设置为红色
  110.     void setRed();
  111.     //设置为黄色
  112.     void setYellow();
  113.     //设置为黑色
  114.     void setBlack();
  115.     //设置为灰色
  116.     void setGray();
  117.     //设置为蓝色
  118.     void setBlue();
  119.     //设置为淡蓝色
  120.     void setLightBlue();
  121.     //设置为淡红色
  122.     void setLightRed();
  123.     //设置为淡绿色
  124.     void setLightGreen();
  125.     //设置报警闪烁
  126.     void startAlarm();
  127.     void stopAlarm();
  128.     void alarm();
  129. };
  130. #endif // LIGHTBUTTON_H

五、完整代码
  1. #pragma execution_character_set("utf-8")
  2. #include "lightbutton.h"
  3. #include "qpainter.h"
  4. #include "qevent.h"
  5. #include "qtimer.h"
  6. #include "qdebug.h"
  7. LightButton::LightButton(QWidget *parent) : QWidget(parent)
  8. {
  9.     text = "";
  10.     textColor = QColor(255, 255, 255);
  11.     alarmColor = QColor(255, 107, 107);
  12.     normalColor = QColor(10, 10, 10);
  13.     borderOutColorStart = QColor(255, 255, 255);
  14.     borderOutColorEnd = QColor(166, 166, 166);
  15.     borderInColorStart = QColor(166, 166, 166);
  16.     borderInColorEnd = QColor(255, 255, 255);
  17.     bgColor = QColor(100, 184, 255);
  18.     showRect = false;
  19.     showOverlay = true;
  20.     overlayColor = QColor(255, 255, 255);
  21.     canMove = false;
  22.     this->installEventFilter(this);
  23.     timerAlarm = new QTimer(this);
  24.     connect(timerAlarm, SIGNAL(timeout()), this, SLOT(alarm()));
  25.     timerAlarm->setInterval(500);
  26.     //setFont(QFont("Arial", 8));
  27. }
  28. bool LightButton::eventFilter(QObject *watched, QEvent *event)
  29. {
  30.     if (canMove) {
  31.         static QPoint lastPnt;
  32.         static bool pressed = false;
  33.         QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  34.         if (mouseEvent->type() == QEvent::MouseButtonPress) {
  35.             if (this->rect().contains(mouseEvent->pos()) && (mouseEvent->button() == Qt::LeftButton)) {
  36.                 lastPnt = mouseEvent->pos();
  37.                 pressed = true;
  38.             }
  39.         } else if (mouseEvent->type() == QEvent::MouseMove && pressed) {
  40.             int dx = mouseEvent->pos().x() - lastPnt.x();
  41.             int dy = mouseEvent->pos().y() - lastPnt.y();
  42.             this->move(this->x() + dx, this->y() + dy);
  43.         } else if (mouseEvent->type() == QEvent::MouseButtonRelease && pressed) {
  44.             pressed = false;
  45.         }
  46.     }
  47.     return QWidget::eventFilter(watched, event);
  48. }
  49. void LightButton::paintEvent(QPaintEvent *)
  50. {
  51.     int width = this->width();
  52.     int height = this->height();
  53.     int side = qMin(width, height);
  54.     //绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放
  55.     QPainter painter(this);
  56.     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
  57.     if (showRect) {
  58.         //绘制矩形区域
  59.         painter.setPen(Qt::NoPen);
  60.         painter.setBrush(bgColor);
  61.         painter.drawRoundedRect(this->rect(), 5, 5);
  62.         //绘制文字
  63.         if (!text.isEmpty()) {
  64.             QFont font;
  65.             font.setPixelSize(side - 20);
  66.             painter.setFont(font);
  67.             painter.setPen(textColor);
  68.             painter.drawText(this->rect(), Qt::AlignCenter, text);
  69.         }
  70.     } else {
  71.         painter.translate(width / 2, height / 2);
  72.         painter.scale(side / 200.0, side / 200.0);
  73.         //绘制外边框
  74.         drawBorderOut(&painter);
  75.         //绘制内边框
  76.         drawBorderIn(&painter);
  77.         //绘制内部指示颜色
  78.         drawBg(&painter);
  79.         //绘制居中文字
  80.         drawText(&painter);
  81.         //绘制遮罩层
  82.         drawOverlay(&painter);
  83.     }
  84. }
  85. void LightButton::drawBorderOut(QPainter *painter)
  86. {
  87.     int radius = 99;
  88.     painter->save();
  89.     painter->setPen(Qt::NoPen);
  90.     QLinearGradient borderGradient(0, -radius, 0, radius);
  91.     borderGradient.setColorAt(0, borderOutColorStart);
  92.     borderGradient.setColorAt(1, borderOutColorEnd);
  93.     painter->setBrush(borderGradient);
  94.     painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
  95.     painter->restore();
  96. }
  97. void LightButton::drawBorderIn(QPainter *painter)
  98. {
  99.     int radius = 90;
  100.     painter->save();
  101.     painter->setPen(Qt::NoPen);
  102.     QLinearGradient borderGradient(0, -radius, 0, radius);
  103.     borderGradient.setColorAt(0, borderInColorStart);
  104.     borderGradient.setColorAt(1, borderInColorEnd);
  105.     painter->setBrush(borderGradient);
  106.     painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
  107.     painter->restore();
  108. }
  109. void LightButton::drawBg(QPainter *painter)
  110. {
  111.     int radius = 80;
  112.     painter->save();
  113.     painter->setPen(Qt::NoPen);
  114.     painter->setBrush(bgColor);
  115.     painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
  116.     painter->restore();
  117. }
  118. void LightButton::drawText(QPainter *painter)
  119. {
  120.     if (text.isEmpty()) {
  121.         return;
  122.     }
  123.     int radius = 100;
  124.     painter->save();
  125.     QFont font;
  126.     font.setPixelSize(85);
  127.     painter->setFont(font);
  128.     painter->setPen(textColor);
  129.     QRect rect(-radius, -radius, radius * 2, radius * 2);
  130.     painter->drawText(rect, Qt::AlignCenter, text);
  131.     painter->restore();
  132. }
  133. void LightButton::drawOverlay(QPainter *painter)
  134. {
  135.     if (!showOverlay) {
  136.         return;
  137.     }
  138.     int radius = 80;
  139.     painter->save();
  140.     painter->setPen(Qt::NoPen);
  141.     QPainterPath smallCircle;
  142.     QPainterPath bigCircle;
  143.     radius -= 1;
  144.     smallCircle.addEllipse(-radius, -radius, radius * 2, radius * 2);
  145.     radius *= 2;
  146.     bigCircle.addEllipse(-radius, -radius + 140, radius * 2, radius * 2);
  147.     //高光的形状为小圆扣掉大圆的部分
  148.     QPainterPath highlight = smallCircle - bigCircle;
  149.     QLinearGradient linearGradient(0, -radius / 2, 0, 0);
  150.     overlayColor.setAlpha(100);
  151.     linearGradient.setColorAt(0.0, overlayColor);
  152.     overlayColor.setAlpha(30);
  153.     linearGradient.setColorAt(1.0, overlayColor);
  154.     painter->setBrush(linearGradient);
  155.     painter->rotate(-20);
  156.     painter->drawPath(highlight);
  157.     painter->restore();
  158. }
  159. QString LightButton::getText() const
  160. {
  161.     return this->text;
  162. }
  163. QColor LightButton::getTextColor() const
  164. {
  165.     return this->textColor;
  166. }
  167. QColor LightButton::getAlarmColor() const
  168. {
  169.     return this->alarmColor;
  170. }
  171. QColor LightButton::getNormalColor() const
  172. {
  173.     return this->normalColor;
  174. }
  175. QColor LightButton::getBorderOutColorStart() const
  176. {
  177.     return this->borderOutColorStart;
  178. }
  179. QColor LightButton::getBorderOutColorEnd() const
  180. {
  181.     return this->borderOutColorEnd;
  182. }
  183. QColor LightButton::getBorderInColorStart() const
  184. {
  185.     return this->borderInColorStart;
  186. }
  187. QColor LightButton::getBorderInColorEnd() const
  188. {
  189.     return this->borderInColorEnd;
  190. }
  191. QColor LightButton::getBgColor() const
  192. {
  193.     return this->bgColor;
  194. }
  195. bool LightButton::getCanMove() const
  196. {
  197.     return this->canMove;
  198. }
  199. bool LightButton::getShowRect() const
  200. {
  201.     return this->showRect;
  202. }
  203. bool LightButton::getShowOverlay() const
  204. {
  205.     return this->showOverlay;
  206. }
  207. QColor LightButton::getOverlayColor() const
  208. {
  209.     return this->overlayColor;
  210. }
  211. QSize LightButton::sizeHint() const
  212. {
  213.     return QSize(100, 100);
  214. }
  215. QSize LightButton::minimumSizeHint() const
  216. {
  217.     return QSize(10, 10);
  218. }
  219. void LightButton::setText(const QString &text)
  220. {
  221.     if (this->text != text) {
  222.         this->text = text;
  223.         update();
  224.     }
  225. }
  226. void LightButton::setTextColor(const QColor &textColor)
  227. {
  228.     if (this->textColor != textColor) {
  229.         this->textColor = textColor;
  230.         update();
  231.     }
  232. }
  233. void LightButton::setAlarmColor(const QColor &alarmColor)
  234. {
  235.     if (this->alarmColor != alarmColor) {
  236.         this->alarmColor = alarmColor;
  237.         update();
  238.     }
  239. }
  240. void LightButton::setNormalColor(const QColor &normalColor)
  241. {
  242.     if (this->normalColor != normalColor) {
  243.         this->normalColor = normalColor;
  244.         update();
  245.     }
  246. }
  247. void LightButton::setBorderOutColorStart(const QColor &borderOutColorStart)
  248. {
  249.     if (this->borderOutColorStart != borderOutColorStart) {
  250.         this->borderOutColorStart = borderOutColorStart;
  251.         update();
  252.     }
  253. }
  254. void LightButton::setBorderOutColorEnd(const QColor &borderOutColorEnd)
  255. {
  256.     if (this->borderOutColorEnd != borderOutColorEnd) {
  257.         this->borderOutColorEnd = borderOutColorEnd;
  258.         update();
  259.     }
  260. }
  261. void LightButton::setBorderInColorStart(const QColor &borderInColorStart)
  262. {
  263.     if (this->borderInColorStart != borderInColorStart) {
  264.         this->borderInColorStart = borderInColorStart;
  265.         update();
  266.     }
  267. }
  268. void LightButton::setBorderInColorEnd(const QColor &borderInColorEnd)
  269. {
  270.     if (this->borderInColorEnd != borderInColorEnd) {
  271.         this->borderInColorEnd = borderInColorEnd;
  272.         update();
  273.     }
  274. }
  275. void LightButton::setBgColor(const QColor &bgColor)
  276. {
  277.     if (this->bgColor != bgColor) {
  278.         this->bgColor = bgColor;
  279.         update();
  280.     }
  281. }
  282. void LightButton::setCanMove(bool canMove)
  283. {
  284.     if (this->canMove != canMove) {
  285.         this->canMove = canMove;
  286.         update();
  287.     }
  288. }
  289. void LightButton::setShowRect(bool showRect)
  290. {
  291.     if (this->showRect != showRect) {
  292.         this->showRect = showRect;
  293.         update();
  294.     }
  295. }
  296. void LightButton::setShowOverlay(bool showOverlay)
  297. {
  298.     if (this->showOverlay != showOverlay) {
  299.         this->showOverlay = showOverlay;
  300.         update();
  301.     }
  302. }
  303. void LightButton::setOverlayColor(const QColor &overlayColor)
  304. {
  305.     if (this->overlayColor != overlayColor) {
  306.         this->overlayColor = overlayColor;
  307.         update();
  308.     }
  309. }
  310. void LightButton::setGreen()
  311. {
  312.     textColor = QColor(255, 255, 255);
  313.     setBgColor(QColor(0, 166, 0));
  314. }
  315. void LightButton::setRed()
  316. {
  317.     textColor = QColor(255, 255, 255);
  318.     setBgColor(QColor(255, 0, 0));
  319. }
  320. void LightButton::setYellow()
  321. {
  322.     textColor = QColor(25, 50, 7);
  323.     setBgColor(QColor(238, 238, 0));
  324. }
  325. void LightButton::setBlack()
  326. {
  327.     textColor = QColor(255, 255, 255);
  328.     setBgColor(QColor(10, 10, 10));
  329. }
  330. void LightButton::setGray()
  331. {
  332.     textColor = QColor(255, 255, 255);
  333.     setBgColor(QColor(129, 129, 129));
  334. }
  335. void LightButton::setBlue()
  336. {
  337.     textColor = QColor(255, 255, 255);
  338.     setBgColor(QColor(0, 0, 166));
  339. }
  340. void LightButton::setLightBlue()
  341. {
  342.     textColor = QColor(255, 255, 255);
  343.     setBgColor(QColor(100, 184, 255));
  344. }
  345. void LightButton::setLightRed()
  346. {
  347.     textColor = QColor(255, 255, 255);
  348.     setBgColor(QColor(255, 107, 107));
  349. }
  350. void LightButton::setLightGreen()
  351. {
  352.     textColor = QColor(255, 255, 255);
  353.     setBgColor(QColor(24, 189, 155));
  354. }
  355. void LightButton::startAlarm()
  356. {
  357.     if (!timerAlarm->isActive()) {
  358.         timerAlarm->start();
  359.     }
  360. }
  361. void LightButton::stopAlarm()
  362. {
  363.     if (timerAlarm->isActive()) {
  364.         timerAlarm->stop();
  365.     }
  366. }
  367. void LightButton::alarm()
  368. {
  369.     static bool isAlarm = false;
  370.     if (isAlarm) {
  371.         textColor = QColor(255, 255, 255);
  372.         bgColor = normalColor;
  373.     } else {
  374.         textColor = QColor(255, 255, 255);
  375.         bgColor = alarmColor;        
  376.     }
  377.     this->update();
  378.     isAlarm = !isAlarm;
  379. }

六、控件介绍
1. 超过149个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,flatui、高亮按钮、滑动选择器、农历等。远超qwt集成的控件数量。
2. 每个类都可以独立成一个单独的控件,零耦合,每个控件一个头文件和一个实现文件,不依赖其他文件,方便单个控件以源码形式集成到项目中,较少代码量。qwt的控件类环环相扣,高度耦合,想要使用其中一个控件,必须包含所有的代码。
3. 全部纯Qt编写,QWidget+QPainter绘制,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等编译器,支持任意操作系统比如windows+linux+mac+嵌入式linux等,不乱码,可直接集成到Qt  Creator中,和自带的控件一样使用,大部分效果只要设置几个属性即可,极为方便。
4. 每个控件都有一个对应的单独的包含该控件源码的DEMO,方便参考使用。同时还提供一个所有控件使用的集成的DEMO。
5. 每个控件的源代码都有详细中文注释,都按照统一设计规范编写,方便学习自定义控件的编写。
6. 每个控件默认配色和demo对应的配色都非常精美。
7. 超过130个可见控件,6个不可见控件。
8. 部分控件提供多种样式风格选择,多种指示器样式选择。
9. 所有控件自适应窗体拉伸变化。
10.  集成自定义控件属性设计器,支持拖曳设计,所见即所得,支持导入导出xml格式。
11. 自带activex控件demo,所有控件可以直接运行在ie浏览器中。
12. 集成fontawesome图形字体+阿里巴巴iconfont收藏的几百个图形字体,享受图形字体带来的乐趣。
13. 所有控件最后生成一个dll动态库文件,可以直接集成到qtcreator中拖曳设计使用。
14. 目前已经有qml版本,后期会考虑出pyqt版本,如果用户需求量很大的话。

七、SDK下载
- SDK下载链接:https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ 提取码:877p
- 下载链接中包含了各个版本的动态库文件,所有控件的头文件,使用demo,自定义控件+属性设计器。
- 自定义控件插件开放动态库dll使用(永久免费),无任何后门和限制,请放心使用。
- 目前已提供26个版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
- 不定期增加控件和完善控件,不定期更新SDK,欢迎各位提出建议,谢谢!
- widget版本(QQ:517216493)qml版本(QQ:373955953)三峰驼(QQ:278969898)。
- 涛哥的知乎专栏 Qt进阶之路 https://zhuanlan.zhihu.com/TaoQt
- 欢迎关注微信公众号【高效程序员】,C++/Python、学习方法、写作技巧、热门技术、职场发展等内容,干货多多,福利多多!
  
欢迎关注微信公众号:Qt实战 (各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发)QQ:517216493  WX:feiyangqingyun  QQ群:751439350
快速回复
限100 字节
 
上一个 下一个