• 2249阅读
  • 4回复

Qt编写自定义控件45-柱状标尺控件 [复制链接]

上一主题 下一主题
离线liudianwu
 

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

一、前言
这个控件写了很久了,是最早期的一批控件中的一个,和温度计控件类似,都是垂直的进度条,可以设置不同的背景颜色,左侧的刻度也可以自由设定,还提供了动画效果,其实就是开启定时器慢慢的进度到设定的目标值,如果设定的值比当前值大,则递增,反之递减。由于当时的qpainter绘制功底还不够如火纯情,所以当时的刻度尺部分都是定死的字体大小,并不会随着控件变化而增大。

二、实现的功能
* 1:可设置精确度(小数点后几位)和间距
* 2:可设置背景色/柱状颜色/线条颜色
* 3:可设置长线条步长及短线条步长
* 4:可启用动画及设置动画步长
* 5:可设置范围值
* 6:支持负数刻度值

三、效果图



四、头文件代码
  1. #ifndef RULERBAR_H
  2. #define RULERBAR_H
  3. /**
  4. * 柱状标尺控件 作者:feiyangqingyun(QQ:517216493) 2016-10-28
  5. * 本控件来源于网络(原作者:kimtaikee(http://www.qtcn.org/bbs/read-htm-tid-33693-ds-1.html#tpc))
  6. * 1:可设置精确度(小数点后几位)和间距
  7. * 2:可设置背景色/柱状颜色/线条颜色
  8. * 3:可设置长线条步长及短线条步长
  9. * 4:可启用动画及设置动画步长
  10. * 5:可设置范围值
  11. * 6:支持负数刻度值
  12. */
  13. #include <QWidget>
  14. #ifdef quc
  15. #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
  16. #include <QtDesigner/QDesignerExportWidget>
  17. #else
  18. #include <QtUiPlugin/QDesignerExportWidget>
  19. #endif
  20. class QDESIGNER_WIDGET_EXPORT RulerBar : public QWidget
  21. #else
  22. class RulerBar : public QWidget
  23. #endif
  24. {
  25.     Q_OBJECT    
  26.     Q_PROPERTY(double minValue READ getMinValue WRITE setMinValue)
  27.     Q_PROPERTY(double maxValue READ getMaxValue WRITE setMaxValue)
  28.     Q_PROPERTY(double value READ getValue WRITE setValue)
  29.     Q_PROPERTY(int precision READ getPrecision WRITE setPrecision)
  30.     Q_PROPERTY(int longStep READ getLongStep WRITE setLongStep)
  31.     Q_PROPERTY(int shortStep READ getShortStep WRITE setShortStep)
  32.     Q_PROPERTY(int space READ getSpace WRITE setSpace)
  33.     Q_PROPERTY(bool animation READ getAnimation WRITE setAnimation)
  34.     Q_PROPERTY(double animationStep READ getAnimationStep WRITE setAnimationStep)
  35.     Q_PROPERTY(QColor bgColorStart READ getBgColorStart WRITE setBgColorStart)
  36.     Q_PROPERTY(QColor bgColorEnd READ getBgColorEnd WRITE setBgColorEnd)
  37.     Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor)
  38.     Q_PROPERTY(QColor barBgColor READ getBarBgColor WRITE setBarBgColor)
  39.     Q_PROPERTY(QColor barColor READ getBarColor WRITE setBarColor)
  40. public:
  41.     explicit RulerBar(QWidget *parent = 0);
  42.     ~RulerBar();
  43. protected:
  44.     void paintEvent(QPaintEvent *);
  45.     void drawBg(QPainter *painter);
  46.     void drawRuler(QPainter *painter);
  47.     void drawBarBg(QPainter *painter);
  48.     void drawBar(QPainter *painter);
  49. private:    
  50.     double minValue;                //最小值
  51.     double maxValue;                //最大值
  52.     double value;                   //目标值
  53.     int precision;                  //精确度,小数点后几位
  54.     int longStep;                   //长线条等分步长
  55.     int shortStep;                  //短线条等分步长
  56.     int space;                      //间距
  57.     bool animation;                 //是否启用动画显示
  58.     double animationStep;           //动画显示时步长
  59.     QColor bgColorStart;            //背景渐变开始颜色
  60.     QColor bgColorEnd;              //背景渐变结束颜色
  61.     QColor lineColor;               //线条颜色
  62.     QColor barBgColor;              //柱状背景色
  63.     QColor barColor;                //柱状颜色
  64.     bool reverse;                   //是否倒退
  65.     double currentValue;            //当前值
  66.     QTimer *timer;                  //定时器绘制动画
  67.     QRectF barRect;                 //柱状图区域
  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.     int getSpace()                  const;
  78.     bool getAnimation()             const;
  79.     double getAnimationStep()       const;
  80.     QColor getBgColorStart()        const;
  81.     QColor getBgColorEnd()          const;
  82.     QColor getLineColor()           const;
  83.     QColor getBarBgColor()          const;
  84.     QColor getBarColor()            const;
  85.     QSize sizeHint()                const;
  86.     QSize minimumSizeHint()         const;
  87. public Q_SLOTS:
  88.     //设置最大最小值-范围值
  89.     void setRange(double minValue, double maxValue);
  90.     void setRange(int minValue, int maxValue);
  91.     //设置最大最小值
  92.     void setMinValue(double minValue);
  93.     void setMaxValue(double maxValue);
  94.     //设置目标值
  95.     void setValue(double value);
  96.     void setValue(int value);
  97.     //设置精确度
  98.     void setPrecision(int precision);
  99.     //设置线条等分步长
  100.     void setLongStep(int longStep);
  101.     void setShortStep(int shortStep);
  102.     //设置间距
  103.     void setSpace(int space);
  104.     //设置是否启用动画显示
  105.     void setAnimation(bool animation);
  106.     //设置动画显示的步长
  107.     void setAnimationStep(double animationStep);
  108.     //设置背景颜色
  109.     void setBgColorStart(const QColor &bgColorStart);
  110.     void setBgColorEnd(const QColor &bgColorEnd);
  111.     //设置线条颜色
  112.     void setLineColor(const QColor &lineColor);
  113.     //设置柱状颜色
  114.     void setBarBgColor(const QColor &barBgColor);
  115.     void setBarColor(const QColor &barColor);
  116. Q_SIGNALS:
  117.     void valueChanged(double value);
  118. };
  119. #endif // RULERBAR_H

五、核心代码
  1. void RulerBar::paintEvent(QPaintEvent *)
  2. {
  3.     //绘制准备工作,启用反锯齿
  4.     QPainter painter(this);
  5.     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
  6.     //绘制背景
  7.     drawBg(&painter);
  8.     //绘制标尺
  9.     drawRuler(&painter);
  10.     //绘制柱状背景
  11.     drawBarBg(&painter);
  12.     //绘制柱状
  13.     drawBar(&painter);
  14. }
  15. void RulerBar::drawBg(QPainter *painter)
  16. {
  17.     painter->save();
  18.     painter->setPen(Qt::NoPen);
  19.     QLinearGradient bgGradient(QPointF(0, 0), QPointF(0, height()));
  20.     bgGradient.setColorAt(0.0, bgColorStart);
  21.     bgGradient.setColorAt(1.0, bgColorEnd);
  22.     painter->setBrush(bgGradient);
  23.     painter->drawRect(rect());
  24.     painter->restore();
  25. }
  26. void RulerBar::drawRuler(QPainter *painter)
  27. {
  28.     painter->save();
  29.     painter->setPen(lineColor);
  30.     //绘制纵向标尺线 20的长度为刻度尺文字的宽度
  31.     double initX = space + 20;
  32.     double initY = space;
  33.     QPointF topPot(initX, initY);
  34.     QPointF bottomPot(initX, height() - space);
  35.     painter->drawLine(topPot, bottomPot);
  36.     //绘制纵向标尺刻度
  37.     double length = height() - 2 * space;
  38.     //计算每一格移动多少
  39.     double increment = length / (maxValue - minValue);
  40.     //长线条短线条长度
  41.     int longLineLen = 10;
  42.     int shortLineLen = 7;
  43.     //根据范围值绘制刻度值及刻度值
  44.     for (int i = maxValue; i >= minValue; i = i - shortStep) {
  45.         if (i % longStep == 0) {
  46.             QPointF leftPot(initX + longLineLen, initY);
  47.             QPointF rightPot(initX, initY);
  48.             painter->drawLine(leftPot, rightPot);
  49.             QString strValue = QString("%1").arg((double)i, 0, 'f', precision);
  50.             double fontWidth = painter->fontMetrics().width(strValue);
  51.             double fontHeight = painter->fontMetrics().height();
  52.             QPointF textPot(initX - fontWidth - 5, initY + fontHeight / 3);
  53.             painter->drawText(textPot, strValue);
  54.         } else {
  55.             if (i % (longStep / 2) == 0) {
  56.                 shortLineLen = 7;
  57.             } else {
  58.                 shortLineLen = 4;
  59.             }
  60.             QPointF leftPot(initX + shortLineLen, initY);
  61.             QPointF rightPot(initX, initY);
  62.             painter->drawLine(leftPot, rightPot);
  63.         }
  64.         initY += increment * shortStep;
  65.     }
  66.     painter->restore();
  67. }
  68. void RulerBar::drawBarBg(QPainter *painter)
  69. {
  70.     painter->save();
  71.     painter->setPen(Qt::NoPen);
  72.     //20的长度为刻度尺文字的宽度 15为刻度尺到柱状图的宽度
  73.     double initX = space + 20 + 15;
  74.     QPointF topLeftPot(initX, space);
  75.     QPointF bottomRightPot(width() - space , height() - space);
  76.     barRect = QRectF(topLeftPot, bottomRightPot);
  77.     painter->setBrush(barBgColor);
  78.     painter->drawRect(barRect);
  79.     painter->restore();
  80. }
  81. void RulerBar::drawBar(QPainter *painter)
  82. {
  83.     painter->save();
  84.     painter->setPen(Qt::NoPen);
  85.     double barHeight = barRect.height();
  86.     double increment = (double)barHeight / (maxValue - minValue);
  87.     double initY = (currentValue - minValue) * increment;
  88.     QPointF topLeftPot(barRect.topLeft().x(), barRect.bottomLeft().y() - initY);
  89.     QPointF bottomRightPot(barRect.bottomRight());
  90.     QRectF currentRect(topLeftPot, bottomRightPot);
  91.     painter->setBrush(barColor);
  92.     painter->drawRect(currentRect);
  93.     painter->restore();
  94. }

六、控件介绍
1. 超过150个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,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 Creator快速入门》《Qt5编程入门》,Qt进阶书籍推荐官方的《C++ GUI Qt4编程》。
- 强烈推荐程序员自我修养和规划系列书《大话程序员》《程序员的成长课》《解忧程序员》!  
欢迎关注微信公众号:Qt实战 (各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发)QQ:517216493  WX:feiyangqingyun  QQ群:751439350
离线九重水

只看该作者 1楼 发表于: 2019-08-08
老刘快来求我帮你顶贴。
离线liudianwu

只看该作者 2楼 发表于: 2019-08-08
回 九重水 的帖子
九重水:老刘快来求我帮你顶贴。[表情]  (2019-08-08 11:01) 

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

只看该作者 3楼 发表于: 2019-08-09
大佬 你搞错板块了
你这个是代码show 不是作品展
QtQML多多指教开发社区 http://qtclub.heilqt.com
将QtCoding进行到底
关注移动互联网,关注金融
开发跨平台客户端,服务于金融行业
专业定制界面
群号:312125701   373955953(qml控件定做)
离线liudianwu

只看该作者 4楼 发表于: 2019-08-09
回 toby520 的帖子
toby520:大佬 你搞错板块了
你这个是代码show 不是作品展[表情] [表情] [表情] [表情]  (2019-08-09 12:32) 

多多你的工资发了没,今天几号了!
欢迎关注微信公众号:Qt实战 (各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发)QQ:517216493  WX:feiyangqingyun  QQ群:751439350
快速回复
限100 字节
 
上一个 下一个