• 2223阅读
  • 1回复

Qt编写自定义控件32-等待进度条控件 [复制链接]

上一主题 下一主题
离线liudianwu
 

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

一、前言
在各种各样的执行任务界面,有时候需要比较多的时间,需要给出一个直观的等待进度条表示当前正在执行的进度,而不至于懵逼在那里,用户不会觉得程序死了还是干嘛了。
等待进度条有好几种办法,比如直接叫美工做好gif图,用QLabel配合QMovie来加载gif图片,这种方法最简单最省事,或者做好多张进度条的图片,采用定时贴图来实现,这些办法省事归省事,就是还不够灵活,写死了,比如有时候需要更换颜色或者换一种展示形式,又需要美工重新做图了,折磨的要死。当时在写这个等待进度条的时候,就有考虑到集成多种样式进去供用户选择,比如圆弧状风格、旋转圆风格、三角圆弧、线条风格、圆环风格等,一个控件就相当于五六个控件,这个才牛逼一些,而且代码还很完整和精彩。

二、实现的功能
* 1:支持多种等待样式风格 圆弧状风格 旋转圆风格 三角圆弧 线条风格 圆环风格
* 2:可设置范围值和当前值
* 3:可设置前景色背景色
* 4:可设置顺时针逆时针旋转
* 5:支持任意大小缩放
* 6:支持设置旋转速度间隔

三、效果图



四、头文件代码
  1. #ifndef PROGRESSWAIT_H
  2. #define PROGRESSWAIT_H
  3. /**
  4. * 等待进度条控件 作者:feiyangqingyun(QQ:517216493) 2016-10-28
  5. * 1:支持多种等待样式风格 圆弧状风格 旋转圆风格 三角圆弧 线条风格 圆环风格
  6. * 2:可设置范围值和当前值
  7. * 3:可设置前景色背景色
  8. * 4:可设置顺时针逆时针旋转
  9. * 5:支持任意大小缩放
  10. * 6:支持设置旋转速度间隔
  11. */
  12. #include <QWidget>
  13. #ifdef quc
  14. #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
  15. #include <QtDesigner/QDesignerExportWidget>
  16. #else
  17. #include <QtUiPlugin/QDesignerExportWidget>
  18. #endif
  19. class QDESIGNER_WIDGET_EXPORT ProgressWait : public QWidget
  20. #else
  21. class ProgressWait : public QWidget
  22. #endif
  23. {
  24.     Q_OBJECT
  25.     Q_ENUMS(BarStyle)
  26.     Q_PROPERTY(bool clockWise READ getClockWise WRITE setClockWise)
  27.     Q_PROPERTY(bool showPercent READ getShowPercent WRITE setShowPercent)
  28.     Q_PROPERTY(int currentValue READ getCurrentValue WRITE setCurrentValue)
  29.     Q_PROPERTY(int maxValue READ getMaxValue WRITE setMaxValue)
  30.     Q_PROPERTY(int interval READ getInterval WRITE setInterval)
  31.     Q_PROPERTY(BarStyle barStyle READ getBarStyle WRITE setBarStyle)
  32.     Q_PROPERTY(QColor background READ getBackground WRITE setBackground)
  33.     Q_PROPERTY(QColor foreground READ getForeground WRITE setForeground)
  34.     Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)
  35. public:
  36.     enum BarStyle {
  37.         BarStyle_Arc = 0,           //圆弧状风格
  38.         BarStyle_RoundCircle = 1,   //旋转圆风格
  39.         BarStyle_Pie = 2,           //三角圆弧风格
  40.         BarStyle_Line = 3,          //线条风格
  41.         BarStyle_Ring = 4,          //圆环风格
  42.         BarStyle_SingleCircle = 5,  //一个圆闪烁
  43.         BarStyle_DoubleCircle = 6   //两个圆闪烁
  44.     };
  45.     ProgressWait(QWidget *parent = 0);
  46.     ~ProgressWait();
  47. protected:
  48.     void resizeEvent(QResizeEvent *);
  49.     void paintEvent(QPaintEvent *);
  50.     void drawArc(QPainter *painter);
  51.     void drawRoundCircle(QPainter *painter);
  52.     void drawPie(QPainter *painter);
  53.     void drawLine(QPainter *painter);
  54.     void drawRing(QPainter *painter);
  55.     void drawSingleCircle(QPainter *painter);
  56.     void drawDoubleCircle(QPainter *painter);
  57.     void drawValue(QPainter *painter);
  58. private:
  59.     bool clockWise;                 //顺时针逆时针
  60.     bool showPercent;               //显示当前百分比
  61.     int currentValue;               //当前值
  62.     int maxValue;                   //最大值
  63.     int interval;                   //旋转间隔
  64.     int minRadius;                  //最小半径
  65.     int maxRadius;                  //最大半径
  66.     int offsetRadius;               //半径偏移量
  67.     int leftRadius;                 //左边圆半径
  68.     int rightRadius;                //右边圆半径
  69.     bool leftIncrease;              //左边递增
  70.     bool rightIncrease;             //右边递增
  71.     BarStyle barStyle;              //样式
  72.     QColor background;              //背景色
  73.     QColor foreground;              //前景色
  74.     QColor textColor;               //文字颜色
  75.     QTimer *timer;                  //定时器绘制
  76. private:
  77.     double degreesToRadians(double value);
  78. private slots:
  79.     void updateValue();
  80. public:
  81.     bool getClockWise()             const;
  82.     bool getShowPercent()           const;
  83.     int getCurrentValue()           const;
  84.     int getMaxValue()               const;
  85.     int getInterval()               const;
  86.     BarStyle getBarStyle()          const;
  87.     QColor getBackground()          const;
  88.     QColor getForeground()          const;
  89.     QColor getTextColor()           const;
  90.     QSize sizeHint()                const;
  91.     QSize minimumSizeHint()         const;
  92. public Q_SLOTS:
  93.     //设置顺时针逆时针旋转
  94.     void setClockWise(bool clockWise);
  95.     //设置是否显示百分比
  96.     void setShowPercent(bool showPercent);
  97.     //设置当前值
  98.     void setCurrentValue(int currentValue);
  99.     //设置最大值
  100.     void setMaxValue(int maxValue);
  101.     //设置旋转速度间隔
  102.     void setInterval(int interval);
  103.     //设置样式
  104.     void setBarStyle(const BarStyle &barStyle);
  105.     //设置前景色
  106.     void setBackground(const QColor &background);
  107.     //设置前景色
  108.     void setForeground(const QColor &foreground);
  109.     //设置文字颜色
  110.     void setTextColor(const QColor &textColor);
  111. };
  112. #endif // PROGRESSWAIT_H

五、核心代码
  1. void ProgressWait::paintEvent(QPaintEvent *)
  2. {
  3.     int width = this->width();
  4.     int height = this->height();
  5.     int side = qMin(width, height);
  6.     //绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放
  7.     QPainter painter(this);
  8.     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
  9.     painter.translate(width / 2, height / 2);
  10.     painter.scale(side / 200.0, side / 200.0);
  11.     if (barStyle == BarStyle_Arc) {
  12.         drawArc(&painter);
  13.     } else if (barStyle == BarStyle_RoundCircle) {
  14.         drawRoundCircle(&painter);
  15.     } else if (barStyle == BarStyle_Pie) {
  16.         drawPie(&painter);
  17.     } else if (barStyle == BarStyle_Line) {
  18.         drawLine(&painter);
  19.     } else if (barStyle == BarStyle_Ring) {
  20.         drawRing(&painter);
  21.     } else if (barStyle == BarStyle_SingleCircle) {
  22.         drawSingleCircle(&painter);
  23.     } else if (barStyle == BarStyle_DoubleCircle) {
  24.         drawDoubleCircle(&painter);
  25.     }
  26.     drawValue(&painter);
  27. }
  28. void ProgressWait::drawArc(QPainter *painter)
  29. {
  30.     painter->save();
  31.     painter->setPen(Qt::NoPen);
  32.     //计算中心点坐标
  33.     int centerX = 0;
  34.     int centerY = 0;
  35.     int radius = 99;
  36.     int radiusBig = radius / 2;
  37.     int radiusSmall = radius / 6;
  38.     double currentangle = currentValue * (360 / (maxValue + 1));
  39.     if (clockWise) {
  40.         currentangle = -currentangle;
  41.     }
  42.     //绘制八卦大圆1
  43.     painter->setBrush(foreground);
  44.     QPainterPath pathBig1(QPointF(centerX + radius * qCos(degreesToRadians(currentangle)),
  45.                                   centerY - radius * qSin(degreesToRadians(currentangle))));
  46.     pathBig1.arcTo(centerX - radius, centerY - radius, radius * 2, radius * 2, currentangle, 180);
  47.     pathBig1.arcTo(centerX + radiusBig * qCos(degreesToRadians(currentangle + 180)) - radiusBig,
  48.                    centerY - radiusBig * qSin(degreesToRadians(currentangle + 180)) - radiusBig,
  49.                    radiusBig * 2, radiusBig * 2, currentangle + 180, 180);
  50.     pathBig1.arcTo(centerX + radiusBig * qCos(degreesToRadians(currentangle)) - radiusBig,
  51.                    centerY - radiusBig * qSin(degreesToRadians(currentangle)) - radiusBig,
  52.                    radiusBig * 2, radiusBig * 2, currentangle + 180, -180
  53.                   );
  54.     painter->drawPath(pathBig1);
  55.     //绘制八卦大圆2
  56.     painter->setBrush(background);
  57.     QPainterPath pathBig2(QPointF(centerX + radius * qCos(degreesToRadians(currentangle)),
  58.                                   centerY - radius * qSin(degreesToRadians(currentangle))));
  59.     pathBig2.arcTo(centerX - radius, centerY - radius, radius * 2, radius * 2, currentangle, -180);
  60.     pathBig2.arcTo(centerX + radiusBig * qCos(degreesToRadians(currentangle + 180)) - radiusBig,
  61.                    centerY - radiusBig * qSin(degreesToRadians(currentangle + 180)) - radiusBig,
  62.                    radiusBig * 2, radiusBig * 2, currentangle + 180, 180);
  63.     pathBig2.arcTo(centerX + radiusBig * qCos(degreesToRadians(currentangle)) - radiusBig,
  64.                    centerY - radiusBig * qSin(degreesToRadians(currentangle)) - radiusBig,
  65.                    radiusBig * 2, radiusBig * 2, currentangle + 180, -180
  66.                   );
  67.     painter->drawPath(pathBig2);
  68.     //绘制八卦小圆1
  69.     painter->setBrush(foreground);
  70.     QPainterPath pathSmall1;
  71.     pathSmall1.addEllipse(centerX + radiusBig * qCos(degreesToRadians(currentangle)) - radiusSmall,
  72.                           centerY - radiusBig * qSin(degreesToRadians(currentangle)) - radiusSmall,
  73.                           radiusSmall * 2, radiusSmall * 2);
  74.     painter->drawPath(pathSmall1);
  75.     //绘制八卦小圆2
  76.     painter->setBrush(background);
  77.     QPainterPath pathSmall2;
  78.     pathSmall2.addEllipse(centerX + radiusBig * qCos(degreesToRadians(180 + currentangle)) - radiusSmall,
  79.                           centerY - radiusBig * qSin(degreesToRadians(180 + currentangle)) - radiusSmall,
  80.                           radiusSmall * 2, radiusSmall * 2);
  81.     painter->drawPath(pathSmall2);
  82.     painter->restore();
  83. }
  84. void ProgressWait::drawRoundCircle(QPainter *painter)
  85. {
  86.     painter->save();
  87.     painter->setPen(Qt::NoPen);
  88.     int radius = 99;
  89.     int minRadius = radius / 6;
  90.     double angleStep = 360.0 / maxValue;
  91.     double alpha = (double)1 / maxValue;
  92.     if (!clockWise) {
  93.         angleStep = -angleStep;
  94.     }
  95.     //计算中心点坐标
  96.     int centerX = 0;
  97.     int centerY = 0;
  98.     double centerRadius = radius / 1.2;
  99.     for (int i = 0; i < maxValue; i++) {
  100.         double angle = (currentValue + i) * angleStep;
  101.         double initX = centerRadius * qCos(degreesToRadians(angle)) + centerX;
  102.         double initY = centerRadius * qSin(degreesToRadians(angle)) + centerY;
  103.         int value = i * alpha * 255;
  104.         value = value < 30 ? 30 : value;
  105.         foreground.setAlpha(value);
  106.         painter->setBrush(foreground);
  107.         painter->drawEllipse(initX - minRadius, initY - minRadius, minRadius * 2, minRadius * 2);
  108.     }
  109.     painter->restore();
  110. }

六、控件介绍
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
离线thinksun

只看该作者 1楼 发表于: 2019-07-21
顶顶更健康
快速回复
限100 字节
 
上一个 下一个