• 2713阅读
  • 2回复

Qt编写自定义控件2-进度条标尺 [复制链接]

上一主题 下一主题
离线liudianwu
 

图酷模式  只看楼主 倒序阅读 楼主  发表于: 2019-04-20
前言
进度条标尺控件的应用场景一般是需要手动拉动进度,上面有标尺可以看到当前进度,类似于qslider控件,其实就是qslider+qprogressbar的杂交版本,不过我才用的是纯qpainter绘制的方式,这样非常灵活可靠,继承自qwidget,这个控件属于标尺类控件中的一个,就是在刻度尺控件基础上增加了鼠标按下拖动进度的功能。
实现的功能
* 1:可设置精确度(小数点后几位)和间距
* 2:可设置背景色/线条颜色
* 3:可设置长线条步长及短线条步长
* 4:可启用动画及设置动画步长
* 5:可设置范围值
* 6:可设置进度颜色
* 7:支持负数刻度值
* 8:可设置标尺在上面还是下面
* 9:支持直接按下定位进度
效果图


文件代码
  1. #ifndef RULERLINE_H
  2. #define RULERLINE_H
  3. /**
  4. * 进度标尺控件 作者:feiyangqingyun(QQ:517216493) 2019-4-11
  5. * 1:可设置精确度(小数点后几位)和间距
  6. * 2:可设置背景色/线条颜色
  7. * 3:可设置长线条步长及短线条步长
  8. * 4:可启用动画及设置动画步长
  9. * 5:可设置范围值
  10. * 6:可设置进度颜色
  11. * 7:支持负数刻度值
  12. * 8:可设置标尺在上面还是下面
  13. * 9:支持直接按下定位进度
  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 RulerProgress : public QWidget
  23. #else
  24. class RulerProgress : public QWidget
  25. #endif
  26. {
  27.     Q_OBJECT    
  28.     Q_PROPERTY(double minValue READ getMinValue WRITE setMinValue)
  29.     Q_PROPERTY(double maxValue READ getMaxValue WRITE setMaxValue)
  30.     Q_PROPERTY(double value READ getValue WRITE setValue)
  31.     Q_PROPERTY(int precision READ getPrecision WRITE setPrecision)
  32.     Q_PROPERTY(int longStep READ getLongStep WRITE setLongStep)
  33.     Q_PROPERTY(int shortStep READ getShortStep WRITE setShortStep)
  34.     Q_PROPERTY(bool rulerTop READ getRulerTop WRITE setRulerTop)
  35.     Q_PROPERTY(bool animation READ getAnimation WRITE setAnimation)
  36.     Q_PROPERTY(double animationStep READ getAnimationStep WRITE setAnimationStep)
  37.     Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
  38.     Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor)
  39.     Q_PROPERTY(QColor progressColor READ getProgressColor WRITE setProgressColor)
  40. public:
  41.     explicit RulerProgress(QWidget *parent = 0);
  42.     ~RulerProgress();
  43. protected:
  44.     void mousePressEvent(QMouseEvent *);
  45.     void mouseMoveEvent(QMouseEvent *);
  46.     void setPressedValue(QPoint pressedPoint);
  47.     void paintEvent(QPaintEvent *);
  48.     void drawBg(QPainter *painter);    
  49.     void drawProgress(QPainter *painter);
  50.     void drawRulerTop(QPainter *painter);
  51.     void drawRulerBottom(QPainter *painter);
  52. private:    
  53.     double minValue;                //最小值
  54.     double maxValue;                //最大值
  55.     double value;                   //目标值
  56.     int precision;                  //精确度,小数点后几位
  57.     int longStep;                   //长线条等分步长
  58.     int shortStep;                  //短线条等分步长
  59.     bool rulerTop;                  //刻度线在上面
  60.     bool animation;                 //是否启用动画显示
  61.     double animationStep;           //动画显示时步长
  62.     QColor bgColor;                 //背景颜色
  63.     QColor lineColor;               //线条颜色
  64.     QColor progressColor;           //进度颜色
  65.     bool reverse;                   //是否倒退
  66.     double currentValue;            //当前值
  67.     QTimer *timer;                  //定时器绘制动画
  68. private slots:
  69.     void updateValue();
  70. public:    
  71.     double getMinValue()            const;
  72.     double getMaxValue()            const;
  73.     double getValue()               const;
  74.     int getPrecision()              const;
  75.     int getLongStep()               const;
  76.     int getShortStep()              const;
  77.     bool getRulerTop()              const;
  78.     bool getAnimation()             const;
  79.     double getAnimationStep()       const;
  80.     QColor getBgColor()             const;
  81.     QColor getLineColor()           const;
  82.     QColor getProgressColor()       const;
  83.     QSize sizeHint()                const;
  84.     QSize minimumSizeHint()         const;
  85. public Q_SLOTS:
  86.     //设置范围值
  87.     void setRange(double minValue, double maxValue);
  88.     void setRange(int minValue, int maxValue);
  89.     //设置最大最小值
  90.     void setMinValue(double minValue);
  91.     void setMaxValue(double maxValue);
  92.     //设置目标值
  93.     void setValue(double value);
  94.     void setValue(int value);
  95.     //设置精确度
  96.     void setPrecision(int precision);
  97.     //设置线条等分步长
  98.     void setLongStep(int longStep);
  99.     void setShortStep(int shortStep);
  100.     //设置刻度尺在上面
  101.     void setRulerTop(bool rulerTop);
  102.     //设置是否启用动画显示
  103.     void setAnimation(bool animation);
  104.     //设置动画显示的步长
  105.     void setAnimationStep(double animationStep);
  106.     //设置背景颜色
  107.     void setBgColor(const QColor &bgColor);
  108.     //设置线条颜色
  109.     void setLineColor(const QColor &lineColor);
  110.     //设置进度颜色
  111.     void setProgressColor(const QColor &progressColor);
  112. Q_SIGNALS:
  113.     void valueChanged(double value);
  114. };
  115. #endif // RULERLINE_H
核心代码
  1. void RulerProgress::paintEvent(QPaintEvent *)
  2. {
  3.     //绘制准备工作,启用反锯齿
  4.     QPainter painter(this);
  5.     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
  6.     //绘制渐变背景
  7.     drawBg(&painter);
  8.     //绘制进度
  9.     drawProgress(&painter);
  10.     //绘制标尺
  11.     if (rulerTop) {
  12.         drawRulerTop(&painter);
  13.     } else {
  14.         drawRulerBottom(&painter);
  15.     }
  16. }
  17. void RulerProgress::drawBg(QPainter *painter)
  18. {
  19.     painter->save();
  20.     painter->setPen(lineColor);
  21.     painter->setBrush(bgColor);
  22.     painter->drawRect(rect());
  23.     painter->restore();
  24. }
  25. void RulerProgress::drawProgress(QPainter *painter)
  26. {
  27.     painter->save();
  28.     painter->setPen(Qt::NoPen);
  29.     painter->setBrush(progressColor);
  30.     double length = width();
  31.     double increment = length / (maxValue - minValue);
  32.     double initX = (currentValue - minValue) * increment;
  33.     QRect rect(0, 0, initX, height());
  34.     painter->drawRect(rect);
  35.     painter->restore();
  36. }
  37. void RulerProgress::drawRulerTop(QPainter *painter)
  38. {
  39.     painter->save();
  40.     painter->setPen(lineColor);
  41.     double initX = 0;
  42.     //绘制横向标尺上部分底部线
  43.     double initTopY = 0;
  44.     QPointF lineTopLeftPot = QPointF(initX, initTopY);
  45.     QPointF lineTopRightPot = QPointF(width() - initX, initTopY);
  46.     painter->drawLine(lineTopLeftPot, lineTopRightPot);
  47.     //绘制上部分及下部分横向标尺刻度
  48.     double length = width();
  49.     //计算每一格移动多少
  50.     double increment = length / (maxValue - minValue);
  51.     //长线条短线条长度
  52.     int longLineLen = 15;
  53.     int shortLineLen = 10;
  54.     //根据范围值绘制刻度值及刻度值 长线条需要移动10像素 短线条需要移动5像素
  55.     for (int i = minValue; i <= maxValue; i = i + shortStep) {
  56.         if (i % longStep == 0) {
  57.             QPointF topPot = QPointF(initX, initTopY);
  58.             QPointF bottomPot = QPointF(initX, initTopY + longLineLen);
  59.             painter->drawLine(topPot, bottomPot);
  60.             //第一个值和最后一个值不要绘制
  61.             if (i == minValue || i == maxValue) {
  62.                 initX += increment * shortStep;
  63.                 continue;
  64.             }
  65.             QString strValue = QString("%1").arg((double)i, 0, 'f', precision);
  66.             double textWidth = fontMetrics().width(strValue);
  67.             double textHeight = fontMetrics().height();
  68.             QPointF textPot = QPointF(initX - textWidth / 2, initTopY + textHeight + longLineLen);
  69.             painter->drawText(textPot, strValue);
  70.         } else {
  71.             if (i % (longStep / 2) == 0) {
  72.                 shortLineLen = 10;
  73.             } else {
  74.                 shortLineLen = 6;
  75.             }
  76.             QPointF topPot = QPointF(initX, initTopY);
  77.             QPointF bottomPot = QPointF(initX, initTopY + shortLineLen);
  78.             painter->drawLine(topPot, bottomPot);
  79.         }
  80.         initX += increment * shortStep;
  81.     }
  82.     painter->restore();
  83. }
  84. void RulerProgress::drawRulerBottom(QPainter *painter)
  85. {
  86.     painter->save();
  87.     painter->setPen(lineColor);
  88.     double initX = 0;
  89.     //绘制横向标尺下部分底部线
  90.     double initBottomY = height();
  91.     QPointF lineBottomLeftPot = QPointF(initX, initBottomY);
  92.     QPointF lineBottomRightPot = QPointF(width() - initX, initBottomY);
  93.     painter->drawLine(lineBottomLeftPot, lineBottomRightPot);
  94.     //绘制上部分及下部分横向标尺刻度
  95.     double length = width();
  96.     //计算每一格移动多少
  97.     double increment = length / (maxValue - minValue);
  98.     //长线条短线条长度
  99.     int longLineLen = 15;
  100.     int shortLineLen = 10;
  101.     //根据范围值绘制刻度值及刻度值 长线条需要移动10像素 短线条需要移动5像素
  102.     for (int i = minValue; i <= maxValue; i = i + shortStep) {
  103.         if (i % longStep == 0) {
  104.             QPointF topPot = QPointF(initX, initBottomY);
  105.             QPointF bottomPot = QPointF(initX, initBottomY - longLineLen);
  106.             painter->drawLine(topPot, bottomPot);
  107.             //第一个值和最后一个值不要绘制
  108.             if (i == minValue || i == maxValue) {
  109.                 initX += increment * shortStep;
  110.                 continue;
  111.             }
  112.             QString strValue = QString("%1").arg((double)i, 0, 'f', precision);
  113.             double textWidth = fontMetrics().width(strValue);
  114.             double textHeight = fontMetrics().height();
  115.             QPointF textPot = QPointF(initX - textWidth / 2, initBottomY - textHeight / 2 - longLineLen);
  116.             painter->drawText(textPot, strValue);
  117.         } else {
  118.             if (i % (longStep / 2) == 0) {
  119.                 shortLineLen = 10;
  120.             } else {
  121.                 shortLineLen = 6;
  122.             }
  123.             QPointF topPot = QPointF(initX, initBottomY);
  124.             QPointF bottomPot = QPointF(initX, initBottomY - shortLineLen);
  125.             painter->drawLine(topPot, bottomPot);
  126.         }
  127.         initX += increment * shortStep;
  128.     }
  129.     painter->restore();
  130. }

控件介绍
1. 超过130个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,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. 超过120个可见控件,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/14iIecP4QLpYvreM1ZDN-dA  提取码:6rj4
- 属性设计器欣赏:https://pan.baidu.com/s/165aJuS_ukR6VGugbtGaf4g  提取码:6014
- 下载链接中包含了各个版本的动态库文件,所有控件的头文件,使用demo。
- 自定义控件插件开放动态库dll使用(永久免费),无任何后门和限制,请放心使用。
- widget版本(QQ:517216493)qml版本(QQ:373955953)三峰驼(QQ:278969898)。


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

只看该作者 1楼 发表于: 2019-04-20
刘总要放大了
雨田哥: 群号:853086607
QQ: 3246214072

刘典武-feiyangqingyun:专业各种自定义控件编写+UI定制+输入法定制+视频监控+工业控制+仪器仪表+嵌入式linux+各种串口网络通信,童叟无欺,量大从优,欢迎咨询购买定制!QQ:517216493
离线hanheyfon

只看该作者 2楼 发表于: 2019-05-16
    
快速回复
限100 字节
 
上一个 下一个