• 3089阅读
  • 7回复

Qt编写自定义控件17-按钮进度条 [复制链接]

上一主题 下一主题
离线liudianwu
 

图酷模式  只看楼主 倒序阅读 楼主  发表于: 2019-05-09
前言
按钮进度条,顾名思义,表面上长得像一个按钮,单击以后切换成进度条指示按钮单击动作执行的进度,主要用在一些需要直接在按钮执行动作显示对应进度的场景,在很多网页中经常看到这种效果,这个效果有个优点就是直接在原地显示进度条,不占用其他位置,然后提供各种颜色可以设置。近期大屏电子看板程序接近尾声了,文章末尾贴出几张动图效果。

实现的功能
* 1:可设置进度线条宽度+颜色
* 2:可设置边框宽度+颜色
* 3:可设置圆角角度+背景颜色

效果图




文件代码
  1. #ifndef PROGRESSBUTTON_H
  2. #define PROGRESSBUTTON_H
  3. /**
  4. * 按钮进度条控件 作者:倪大侠(QQ:393320854 zyb920@hotmail.com) 2019-4-17
  5. * 1:可设置进度线条宽度+颜色
  6. * 2:可设置边框宽度+颜色
  7. * 3:可设置圆角角度+背景颜色
  8. */
  9. #include <QWidget>
  10. class QTimer;
  11. #ifdef quc
  12. #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
  13. #include <QtDesigner/QDesignerExportWidget>
  14. #else
  15. #include <QtUiPlugin/QDesignerExportWidget>
  16. #endif
  17. class QDESIGNER_WIDGET_EXPORT ProgressButton : public QWidget
  18. #else
  19. class ProgressButton : public QWidget
  20. #endif
  21. {
  22.     Q_OBJECT
  23.     Q_PROPERTY(int lineWidth READ getLineWidth WRITE setLineWidth)
  24.     Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor)
  25.     Q_PROPERTY(int borderWidth READ getBorderWidth WRITE setBorderWidth)
  26.     Q_PROPERTY(QColor borderColor READ getBorderColor WRITE setBorderColor)
  27.     Q_PROPERTY(int borderRadius READ getBorderRadius WRITE setBorderRadius)
  28.     Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
  29. public:
  30.     explicit ProgressButton(QWidget *parent = 0);
  31. protected:
  32.     void resizeEvent(QResizeEvent *);
  33.     void mousePressEvent(QMouseEvent *);
  34.     void paintEvent(QPaintEvent *);
  35.     void drawBg(QPainter *painter);
  36.     void drawProgress(QPainter *painter);
  37. private:
  38.     int lineWidth;          //线条宽度
  39.     QColor lineColor;       //线条颜色
  40.     int borderWidth;        //边框宽度
  41.     QColor borderColor;     //边框颜色
  42.     int borderRadius;       //圆角角度
  43.     QColor bgColor;         //背景颜色
  44.     double value;           //当前值
  45.     int status;             //状态
  46.     int tempWidth;          //动态改变宽度
  47.     QTimer *timer;          //定时器改变进度
  48. public:
  49.     int getLineWidth()      const;
  50.     QColor getLineColor()   const;
  51.     int getBorderWidth()    const;
  52.     QColor getBorderColor() const;
  53.     int getBorderRadius()   const;
  54.     QColor getBgColor()     const;
  55.     QSize sizeHint()        const;
  56.     QSize minimumSizeHint() const;
  57. private slots:
  58.     void progress();
  59. public Q_SLOTS:
  60.     //设置线条宽度+颜色
  61.     void setLineWidth(int lineWidth);
  62.     void setLineColor(const QColor &lineColor);
  63.     //设置边框宽度+颜色
  64.     void setBorderWidth(int borderWidth);
  65.     void setBorderColor(const QColor &borderColor);
  66.     //设置圆角角度+背景颜色
  67.     void setBorderRadius(int borderRadius);
  68.     void setBgColor(const QColor &bgColor);
  69. Q_SIGNALS:
  70.     void valueChanged(int value);
  71. };
  72. #endif // PROGRESSBUTTON_H
核心代码
  1. void ProgressButton::paintEvent(QPaintEvent *)
  2. {
  3.     QPainter painter(this);
  4.     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
  5.     if (1 == status) {
  6.         //绘制当前进度
  7.         drawProgress(&painter);
  8.     } else {
  9.         //绘制按钮背景
  10.         drawBg(&painter);
  11.     }
  12. }
  13. void ProgressButton::drawBg(QPainter *painter)
  14. {
  15.     painter->save();
  16.     int width = this->width();
  17.     int height = this->height();
  18.     int side = qMin(width, height);
  19.     QPen pen;
  20.     pen.setWidth(borderWidth);
  21.     pen.setColor(borderColor);
  22.     painter->setPen(borderWidth > 0 ? pen : Qt::NoPen);
  23.     painter->setBrush(bgColor);
  24.     QRect rect(((width - tempWidth) / 2) + borderWidth, borderWidth, tempWidth - (borderWidth * 2), height - (borderWidth * 2));
  25.     painter->drawRoundedRect(rect, borderRadius, borderRadius);
  26.     QFont font;
  27.     font.setPixelSize(side - 18);
  28.     painter->setFont(font);
  29.     painter->setPen(lineColor);
  30.     painter->drawText(rect, Qt::AlignCenter, status == 2 ? "完 成" : "开 始");
  31.     painter->restore();
  32. }
  33. void ProgressButton::drawProgress(QPainter *painter)
  34. {
  35.     painter->save();
  36.     int width = this->width();
  37.     int height = this->height();
  38.     int side = qMin(width, height);
  39.     int radius = 99 - borderWidth;
  40.     //绘制外圆
  41.     QPen pen;
  42.     pen.setWidth(borderWidth);
  43.     pen.setColor(borderColor);
  44.     painter->setPen(borderWidth > 0 ? pen : Qt::NoPen);
  45.     painter->setBrush(bgColor);
  46.     //平移坐标轴中心,等比例缩放
  47.     QRect rectCircle(-radius, -radius, radius * 2, radius * 2);
  48.     painter->translate(width / 2, height / 2);
  49.     painter->scale(side / 200.0, side / 200.0);
  50.     painter->drawEllipse(rectCircle);
  51.     //绘制圆弧进度
  52.     pen.setWidth(lineWidth);
  53.     pen.setColor(lineColor);
  54.     painter->setPen(pen);
  55.     int offset = radius - lineWidth - 5;
  56.     QRectF rectArc(-offset, -offset, offset * 2, offset * 2);
  57.     int startAngle = offset * 16;
  58.     int spanAngle = -value * 16;
  59.     painter->drawArc(rectArc, startAngle, spanAngle);
  60.     //绘制进度文字
  61.     QFont font;
  62.     font.setPixelSize(offset - 15);
  63.     painter->setFont(font);
  64.     QString strValue = QString("%1%").arg((int)value  * 100 / 360);
  65.     painter->drawText(rectCircle, Qt::AlignCenter, strValue);
  66.     painter->restore();
  67. }
  68. void ProgressButton::progress()
  69. {
  70.     if (0 == status) {
  71.         tempWidth -= 5;
  72.         if (tempWidth < this->height() / 2) {
  73.             tempWidth = this->height() / 2;
  74.             status = 1;
  75.         }
  76.     } else if (1 == status) {
  77.         value += 1.0;
  78.         if (value >= 360) {
  79.             value = 360.0;
  80.             status = 2;
  81.         }
  82.     } else if (2 == status) {
  83.         tempWidth += 5;
  84.         if (tempWidth > this->width()) {
  85.             tempWidth = this->width();
  86.             timer->stop();
  87.         }
  88.     }
  89.     this->update();
  90. }

控件介绍
1. 超过145个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,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使用(永久免费),无任何后门和限制,请放心使用。
- 目前已提供26个版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
- 不定期增加控件和完善控件,不定期更新SDK,欢迎各位提出建议,谢谢!
- widget版本(QQ:517216493)qml版本(QQ:373955953)三峰驼(QQ:278969898)。
  

欢迎关注微信公众号:Qt实战 (各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发)QQ:517216493  WX:feiyangqingyun  QQ群:751439350
离线qibbs

只看该作者 1楼 发表于: 2019-05-09
这个效果必须赞!
春梦觉来心自警,往事般般应。
离线liudianwu

只看该作者 2楼 发表于: 2019-05-09
回 qibbs 的帖子
qibbs:这个效果必须赞![表情]  (2019-05-09 12:51) 

大佬联系方式多少,向你学习!
欢迎关注微信公众号:Qt实战 (各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发)QQ:517216493  WX:feiyangqingyun  QQ群:751439350
离线qiu4466

只看该作者 3楼 发表于: 2019-05-09
大力支持,不错
离线qibbs

只看该作者 4楼 发表于: 2019-05-09
回 liudianwu 的帖子
liudianwu:大佬联系方式多少,向你学习! (2019-05-09 13:22) 

我向你学习!已加你签名下方的qq了
春梦觉来心自警,往事般般应。

只看该作者 5楼 发表于: 2019-05-09
看到了作者小倪大佬
离线nigoole

只看该作者 6楼 发表于: 2019-05-09
回 青春的年代 的帖子
青春的年代:看到了作者小倪大佬 (2019-05-09 17:44)

....就你眼尖,我做了个框架,好像是居士大佬的qml控件,然后我用widget仿了个,最后完善的是刘总!

有句话说得好:好好学习,天天向上。加油~~!有上船的朋友联系企鹅393320854
离线305750665

只看该作者 7楼 发表于: 2019-05-09
回 nigoole 的帖子
nigoole:....就你眼尖,我做了个框架,好像是居士大佬的qml控件,然后我用widget仿了个,最后完善的是刘总!
 (2019-05-09 18:29) 

倪总一向的谦虚。但是特服刘总,刘总总是完善得很完美
雨田哥: 群号:853086607
QQ: 3246214072

刘典武-feiyangqingyun:专业各种自定义控件编写+UI定制+输入法定制+视频监控+工业控制+仪器仪表+嵌入式linux+各种串口网络通信,童叟无欺,量大从优,欢迎咨询购买定制!QQ:517216493
快速回复
限100 字节
 
上一个 下一个