找回密码
 立即注册
收起左侧

Qt编写自定义控件13-多态进度条

0
回复
4804
查看
[复制链接]
累计签到:7 天
连续签到:1 天
来源: 2019-4-30 23:25:07 显示全部楼层 |阅读模式
前言
多态进度条,顾名思义,有多重状态,其实本控件主要是用来表示百分比进度的,由于之前已经存在了百分比进度条控件,名字被霸占了,按照先来先得原则,只好另外取个别名叫做多态进度条,应用场景是,某种任务有三种状态,比如正常状态、警戒状态、报警状态,这三种状态都分别有一个占比,需要用不同的颜色表示,这样就衍生出了此控件,类似于堆积图。接下来节假日四天,可以全身心投入研发还未完工的大屏UI程序,基础控件部分+二级界面部分都已经做好,现在专心整合到主界面和打通数据流(采用数据库采集+网络采集两种方式)。多态进度条也是为了此项目特意定制的。
实现的功能
* 1:可设置三种状态不同的值
* 2:可设置三种状态不同的颜色
* 3:可设置圆角角度
* 4:可设置启用自动圆角
* 5:可设置边框宽度+颜色
* 6:可设置是否显示值或者百分比
* 7:可设置字体自适应大小
* 8:可设置背景颜色+文字颜色
* 9:精准计算圆角角度,解决了QSS中border-radius当进度小于圆角角度出现方形的BUG
效果图


头文件代码
  1. #ifndef PROGRESSTHREE_H
  2. #define PROGRESSTHREE_H

  3. /**
  4. * 多态进度条控件 作者:feiyangqingyun(QQ:517216493) 2019-4-30
  5. * 1:可设置三种状态不同的值
  6. * 2:可设置三种状态不同的颜色
  7. * 3:可设置圆角角度
  8. * 4:可设置启用自动圆角
  9. * 5:可设置边框宽度+颜色
  10. * 6:可设置是否显示值或者百分比
  11. * 7:可设置字体自适应大小
  12. * 8:可设置背景颜色+文字颜色
  13. * 9:精准计算圆角角度,解决了QSS中border-radius当进度小于圆角角度出现方形的BUG
  14. */

  15. #include <QWidget>

  16. #ifdef quc
  17. #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
  18. #include <QtDesigner/QDesignerExportWidget>
  19. #else
  20. #include <QtUiPlugin/QDesignerExportWidget>
  21. #endif

  22. class QDESIGNER_WIDGET_EXPORT ProgressThree : public QWidget
  23. #else
  24. class ProgressThree : public QWidget
  25. #endif

  26. {
  27.     Q_OBJECT
  28.     Q_PROPERTY(int value1 READ getValue1 WRITE setValue1)
  29.     Q_PROPERTY(int value2 READ getValue2 WRITE setValue2)
  30.     Q_PROPERTY(int value3 READ getValue3 WRITE setValue3)

  31.     Q_PROPERTY(QColor color1 READ getColor1 WRITE setColor1)
  32.     Q_PROPERTY(QColor color2 READ getColor2 WRITE setColor2)
  33.     Q_PROPERTY(QColor color3 READ getColor3 WRITE setColor3)

  34.     Q_PROPERTY(int radius READ getRadius WRITE setRadius)
  35.     Q_PROPERTY(bool autoRadius READ getAutoRadius WRITE setAutoRadius)

  36.     Q_PROPERTY(bool showValue READ getShowValue WRITE setShowValue)
  37.     Q_PROPERTY(bool showPercent READ getShowPercent WRITE setShowPercent)
  38.     Q_PROPERTY(bool autoFont READ getAutoFont WRITE setAutoFont)

  39.     Q_PROPERTY(double borderWidth READ getBorderWidth WRITE setBorderWidth)
  40.     Q_PROPERTY(QColor borderColor READ getBorderColor WRITE setBorderColor)

  41.     Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
  42.     Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)

  43. public:
  44.     explicit ProgressThree(QWidget *parent = 0);

  45. protected:
  46.     void paintEvent(QPaintEvent *);
  47.     void drawBg(QPainter *painter);
  48.     void drawValue(QPainter *painter);
  49.     void drawValue1(QPainter *painter);
  50.     void drawValue2(QPainter *painter);
  51.     void drawValue3(QPainter *painter);
  52.     void drawBorder(QPainter *painter);

  53. private:
  54.     int value1;                 //值1
  55.     int value2;                 //值2
  56.     int value3;                 //值3

  57.     QColor color1;              //颜色1
  58.     QColor color2;              //颜色2
  59.     QColor color3;              //颜色3

  60.     int radius;                 //圆角角度
  61.     bool autoRadius;            //自动圆角

  62.     bool showValue;             //显示对应的值
  63.     bool showPercent;           //显示对应的百分比
  64.     bool autoFont;              //自动字体大小

  65.     double borderWidth;         //边框宽度
  66.     QColor borderColor;         //边框颜色

  67.     QColor bgColor;             //背景颜色
  68.     QColor textColor;           //文字颜色

  69.     int width1;                 //值1宽度
  70.     int width2;                 //值2宽度
  71.     int width3;                 //值3宽度   

  72. public:
  73.     int getValue1()             const;
  74.     int getValue2()             const;
  75.     int getValue3()             const;

  76.     QColor getColor1()          const;
  77.     QColor getColor2()          const;
  78.     QColor getColor3()          const;

  79.     int getRadius()             const;
  80.     bool getAutoRadius()        const;

  81.     bool getShowValue()         const;
  82.     bool getShowPercent()       const;
  83.     bool getAutoFont()          const;

  84.     double getBorderWidth()     const;
  85.     QColor getBorderColor()     const;

  86.     QColor getBgColor()         const;
  87.     QColor getTextColor()       const;

  88.     QSize sizeHint()            const;
  89.     QSize minimumSizeHint()     const;

  90. public Q_SLOTS:
  91.     //设置三个值
  92.     void setValue1(int value1);
  93.     void setValue2(int value2);
  94.     void setValue3(int value3);

  95.     //设置三个颜色
  96.     void setColor1(const QColor &color1);
  97.     void setColor2(const QColor &color2);
  98.     void setColor3(const QColor &color3);

  99.     //设置圆角+自动圆角
  100.     void setRadius(int radius);
  101.     void setAutoRadius(bool autoRadius);

  102.     //设置显示值+显示百分比+自动字体大小
  103.     void setShowValue(bool showValue);
  104.     void setShowPercent(bool showPercent);
  105.     void setAutoFont(bool autoFont);

  106.     //设置边框宽度+颜色
  107.     void setBorderWidth(double borderWidth);
  108.     void setBorderColor(const QColor &borderColor);

  109.     //设置背景颜色+文字颜色
  110.     void setBgColor(const QColor &bgColor);
  111.     void setTextColor(const QColor &textColor);
  112. };

  113. #endif // PROGRESSTHREE_H
复制代码
核心代码

  1. void ProgressThree::paintEvent(QPaintEvent *)
  2. {
  3.     //绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放
  4.     QPainter painter(this);
  5.     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

  6.     if (autoRadius) {
  7.         radius = this->height() / 2;
  8.     }

  9.     //绘制背景
  10.     drawBg(&painter);
  11.     //绘制值1
  12.     drawValue1(&painter);
  13.     //绘制值2
  14.     drawValue2(&painter);
  15.     //绘制值3
  16.     drawValue3(&painter);
  17.     //最后绘制边框盖上去
  18.     drawBorder(&painter);
  19. }

  20. void ProgressThree::drawBg(QPainter *painter)
  21. {
  22.     painter->save();
  23.     painter->setPen(Qt::NoPen);
  24.     painter->setBrush(bgColor);
  25.     painter->drawRoundedRect(this->rect(), radius, radius);
  26.     painter->restore();
  27. }

  28. void ProgressThree::drawValue1(QPainter *painter)
  29. {
  30.     painter->save();

  31.     //计算百分比以及对应的宽度
  32.     int sum = value1 + value2 + value3;
  33.     double percent = (double)value1 / sum;
  34.     width1 = this->width() * percent;

  35.     painter->setPen(Qt::NoPen);
  36.     painter->setBrush(color1);

  37.     //计算绘制的区域,需要裁剪圆角部分
  38.     QPainterPath clipPath;
  39.     clipPath.addRoundedRect(this->rect(), radius, radius);
  40.     painter->setClipPath(clipPath);
  41.     QRect rect(0, 0, width1, this->height());
  42.     painter->drawRect(rect);

  43.     //绘制文字
  44.     if (showValue) {
  45.         //设置文字字体+颜色
  46.         if (autoFont) {
  47.             QFont font;
  48.             font.setPixelSize(this->height() * 0.9);
  49.             painter->setFont(font);
  50.         }

  51.         QString text = QString::number(value1);
  52.         if (showPercent) {
  53.             text = QString("%1%").arg(QString::number(percent * 100, 'f', 0));
  54.         }

  55.         painter->setPen(textColor);
  56.         painter->drawText(rect, Qt::AlignCenter, text);
  57.     }

  58.     painter->restore();
  59. }

  60. void ProgressThree::drawValue2(QPainter *painter)
  61. {
  62.     painter->save();

  63.     //计算百分比以及对应的宽度
  64.     int sum = value1 + value2 + value3;
  65.     double percent = (double)value2 / sum;   
  66.     width2 = this->width() * percent;

  67.     painter->setPen(Qt::NoPen);
  68.     painter->setBrush(color2);

  69.     //计算绘制的区域,需要裁剪圆角部分
  70.     QPainterPath clipPath;
  71.     clipPath.addRoundedRect(this->rect(), radius, radius);
  72.     painter->setClipPath(clipPath);
  73.     QRect rect(width1, 0, width2, this->height());
  74.     painter->drawRect(rect);

  75.     //绘制文字
  76.     if (showValue) {
  77.         //设置文字字体+颜色
  78.         if (autoFont) {
  79.             QFont font;
  80.             font.setPixelSize(this->height() * 0.9);
  81.             painter->setFont(font);
  82.         }

  83.         QString text = QString::number(value2);
  84.         if (showPercent) {
  85.             text = QString("%1%").arg(QString::number(percent * 100, 'f', 0));
  86.         }

  87.         painter->setPen(textColor);
  88.         painter->drawText(rect, Qt::AlignCenter, text);
  89.     }

  90.     painter->restore();
  91. }

  92. void ProgressThree::drawValue3(QPainter *painter)
  93. {
  94.     painter->save();

  95.     //宽度减去其他两个就是
  96.     int sum = value1 + value2 + value3;
  97.     double percent = (double)value3 / sum;
  98.     width3 = this->width() - width1 - width2;

  99.     painter->setPen(Qt::NoPen);
  100.     painter->setBrush(color3);

  101.     //计算绘制的区域,需要裁剪圆角部分
  102.     QPainterPath clipPath;
  103.     clipPath.addRoundedRect(this->rect(), radius, radius);
  104.     painter->setClipPath(clipPath);
  105.     QRect rect(width1 + width2, 0, width3, this->height());
  106.     painter->drawRect(rect);

  107.     //绘制文字
  108.     if (showValue) {
  109.         //设置文字字体+颜色
  110.         if (autoFont) {
  111.             QFont font;
  112.             font.setPixelSize(this->height() * 0.9);
  113.             painter->setFont(font);
  114.         }

  115.         QString text = QString::number(value3);
  116.         if (showPercent) {
  117.             text = QString("%1%").arg(QString::number(percent * 100, 'f', 0));
  118.         }

  119.         painter->setPen(textColor);
  120.         painter->drawText(rect, Qt::AlignCenter, text);
  121.     }

  122.     painter->restore();
  123. }

  124. void ProgressThree::drawBorder(QPainter *painter)
  125. {
  126.     painter->save();

  127.     QPen pen;
  128.     pen.setWidthF(borderWidth);
  129.     pen.setColor(borderColor);
  130.     painter->setPen(borderWidth > 0 ? pen : Qt::NoPen);
  131.     painter->setBrush(Qt::NoBrush);

  132.     int radius = this->radius;
  133.     if (autoRadius) {
  134.         radius = this->height() / 2;
  135.     }

  136.     //绘制圆角矩形
  137.     painter->drawRoundedRect(this->rect(), radius, radius);

  138.     painter->restore();
  139. }
复制代码


控件介绍
1. 超过140个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,flatui、高亮按钮、滑动选择器、农历等。远超qwt集成的控件数量。
2. 每个类都可以独立成一个单独的控件,零耦合,每个控件一个头文件和一个实现文件,不依赖其他文件,方便单个控件以源码形式集成到项目中,较少代码量。qwt的控件类环环相扣,高度耦合,想要使用其中一个控件,必须包含所有的代码。
3. 全部纯Qt编写,QWidget+QPainter绘制,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等编译器,不乱码,可直接集成到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中拖曳设计使用。
SDK下载
- SDK下载链接:https://pan.baidu.com/s/1tD9v1YPfE2fgYoK6lqUr1Q 提取码:lyhk
- 自定义控件+属性设计器欣赏:https://pan.baidu.com/s/1l6L3rKSiLu_uYi7lnL3ibQ 提取码:tmvl
- 下载链接中包含了各个版本的动态库文件,所有控件的头文件,使用demo。
- 自定义控件插件开放动态库dll使用(永久免费),无任何后门和限制,请放心使用。
- 目前已提供22个版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
- 不定期增加控件和完善控件,不定期更新SDK,欢迎各位提出建议,谢谢!
- widget版本(QQ:517216493)qml版本(QQ:373955953)三峰驼(QQ:278969898)。



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了