• 2310阅读
  • 6回复

Qt编写自定义控件41-自定义环形图 [复制链接]

上一主题 下一主题
离线liudianwu
 

只看楼主 正序阅读 楼主  发表于: 2019-08-04

一、前言
自定义环形图控件类似于自定义饼状图控件,也是提供一个饼图区域展示占比,其实核心都是根据自动计算到的百分比绘制饼图区域。当前环形图控件模仿的是echart中的环形图控件,提供双层环形图,有一层外圈的环形图,还有一层里边的饼状图,相当于一个控件就可以表示两种类型的占比,这样涵盖的信息量更大,而且提供了鼠标移上去自动突出显示的功能,下面的图例也跟着加粗高亮显示,非常直观,类似的控件在很多web项目中大量运用。
本控件的难点并不是绘制环形或者饼图区域,初学者都会,难点在如何自动精准计算鼠标所在区域,然后高亮突出显示,用的是QPainterPath的contains方法判断当前鼠标在哪个区域,需要在绘制的时候记住该饼图区域的QPainterPath,然后在mouseMoveEvent中判断,需要开启鼠标捕捉。控件原作者雨田哥(https://blog.csdn.net/ly305750665

二、实现的功能
* 1:可设置是否显示标题+标题文字+标题高度+标题字号
* 2:可设置是否显示图例+图例高度+图例字号
* 3:可设置背景颜色+文字颜色+高亮颜色+标识颜色
* 4:可设置外圆颜色+中间圆颜色+内圆颜色
* 5:可设置外圆数据集合+内圆数据集合
* 6:鼠标悬停突出显示区域并高亮显示文字
* 7:每个区域都可设置对应的颜色+文字描述+百分比
* 8:支持直接字符串设置文字集合和百分比集合

三、效果图



四、头文件代码
  1. #ifndef CUSTOMRING_H
  2. #define CUSTOMRING_H
  3. /**
  4. * 自定义环形图控件 整理:feiyangqingyun(QQ:517216493) 2019-7-28
  5. * 原作者:雨田哥(QQ:3246214072)
  6. * 1:可设置是否显示标题+标题文字+标题高度+标题字号
  7. * 2:可设置是否显示图例+图例高度+图例字号
  8. * 3:可设置背景颜色+文字颜色+高亮颜色+标识颜色
  9. * 4:可设置外圆颜色+中间圆颜色+内圆颜色
  10. * 5:可设置外圆数据集合+内圆数据集合
  11. * 6:鼠标悬停突出显示区域并高亮显示文字
  12. * 7:每个区域都可设置对应的颜色+文字描述+百分比
  13. * 8:支持直接字符串设置文字集合和百分比集合
  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 CustomRing : public QWidget
  23. #else
  24. class CustomRing : public QWidget
  25. #endif
  26. {
  27.     Q_OBJECT
  28.     Q_PROPERTY(bool showTitle READ getShowTitle WRITE setShowTitle)
  29.     Q_PROPERTY(int titleHeight READ getTitleHeight WRITE setTitleHeight)
  30.     Q_PROPERTY(int titleFontSize READ getTitleFontSize WRITE setTitleFontSize)
  31.     Q_PROPERTY(QString title READ getTitle WRITE setTitle)
  32.     Q_PROPERTY(bool showLegend READ getShowLegend WRITE setShowLegend)
  33.     Q_PROPERTY(int legendHeight READ getLegendHeight WRITE setLegendHeight)
  34.     Q_PROPERTY(int legendFontSize READ getLegendFontSize WRITE setLegendFontSize)
  35.     Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
  36.     Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)
  37.     Q_PROPERTY(QColor highColor READ getHighColor WRITE setHighColor)
  38.     Q_PROPERTY(QColor flagColor READ getFlagColor WRITE setFlagColor)
  39.     Q_PROPERTY(QColor outCircleColor READ getOutCircleColor WRITE setOutCircleColor)
  40.     Q_PROPERTY(QColor midCircleColor READ getMidCircleColor WRITE setMidCircleColor)
  41.     Q_PROPERTY(QColor inCircleColor READ getInCircleColor WRITE setInCircleColor)
  42.     Q_PROPERTY(QString outPieInfos READ getOutPieInfos WRITE setOutPieInfos)
  43.     Q_PROPERTY(QString inPieInfos READ getInPieInfos WRITE setInPieInfos)
  44. public:
  45.     struct RingData {
  46.         int offset;         //鼠标移上去往外边突出显示的偏移距离
  47.         int percent;        //百分比
  48.         QColor color;       //背景色
  49.         QString text;       //文本
  50.         QPainterPath path;  //区域路径
  51.         RingData()
  52.         {
  53.             offset = 0;
  54.             percent = 0;
  55.             color = QColor(0, 192, 133);
  56.             text = "";
  57.         }
  58.     };
  59.     CustomRing(QWidget *parent = 0);
  60.     ~CustomRing();
  61. protected:
  62.     void mouseMoveEvent(QMouseEvent *event);
  63.     void paintEvent(QPaintEvent *);
  64.     void drawBg(QPainter *painter);
  65.     void drawOutCircle(QPainter *painter);
  66.     void drawOutPie(QPainter *painter, qreal scale, QPoint center);
  67.     void drawMidCircle(QPainter *painter);
  68.     void drawInPie(QPainter *painter, qreal scale, QPoint center);
  69.     void drawInCircle(QPainter *painter);
  70.     void drawTitle(QPainter *painter);
  71.     void drawLegendText(QPainter *painter, qreal scale);
  72. private:
  73.     bool showTitle;             //显示标题
  74.     int titleHeight;            //标题高度
  75.     int titleFontSize;          //标题字号
  76.     QString title;              //标题
  77.     bool showLegend;            //显示图例
  78.     int legendHeight;           //图例高度
  79.     int legendFontSize;         //图例字号
  80.     QColor bgColor;             //背景颜色
  81.     QColor textColor;           //文字颜色
  82.     QColor highColor;           //高亮颜色
  83.     QColor flagColor;           //标题左侧标识颜色
  84.     QColor outCircleColor;      //外圆颜色
  85.     QColor midCircleColor;      //中间圆颜色
  86.     QColor inCircleColor;       //里边圆颜色
  87.     QString outPieInfos;        //外边饼图数据
  88.     QString inPieInfos;         //里边饼图数据
  89.     QList<QColor> outPieColors; //饼图颜色集合,在设置字符串时候用
  90.     QList<QColor> inPieColors;  //饼图颜色集合,在设置字符串时候用
  91.     QList<RingData> outPieInfo; //外边饼图数据
  92.     QList<RingData> inPieInfo;  //里边饼图数据
  93. public:
  94.     bool getShowTitle()         const;
  95.     int getTitleHeight()        const;
  96.     int getTitleFontSize()      const;
  97.     QString getTitle()          const;
  98.     bool getShowLegend()        const;
  99.     int getLegendHeight()       const;
  100.     int getLegendFontSize()     const;
  101.     QColor getBgColor()         const;
  102.     QColor getTextColor()       const;
  103.     QColor getHighColor()       const;
  104.     QColor getFlagColor()       const;
  105.     QColor getOutCircleColor()  const;
  106.     QColor getMidCircleColor()  const;
  107.     QColor getInCircleColor()   const;
  108.     QString getOutPieInfos()    const;
  109.     QString getInPieInfos()     const;
  110.     QSize sizeHint()            const;
  111.     QSize minimumSizeHint()     const;
  112. public Q_SLOTS:
  113.     //显示标题+标题栏高度+标题字号+标题文字
  114.     void setShowTitle(bool showTitle);
  115.     void setTitleHeight(int titleHeight);
  116.     void setTitleFontSize(int titleFontSize);
  117.     void setTitle(const QString &title);
  118.     //显示图例+图例高度+图例字号
  119.     void setShowLegend(bool showLegend);
  120.     void setLegendHeight(int legendHeight);
  121.     void setLegendFontSize(int legendFontSize);
  122.     //设置背景颜色+文字颜色+高亮颜色+标识颜色
  123.     void setBgColor(const QColor &bgColor);
  124.     void setTextColor(const QColor &textColor);
  125.     void setHighColor(const QColor &highColor);
  126.     void setFlagColor(const QColor &flagColor);
  127.     //设置外圆颜色+中间圆颜色+里边圆颜色
  128.     void setOutCircleColor(const QColor &outCircleColor);
  129.     void setMidCircleColor(const QColor &midCircleColor);
  130.     void setInCircleColor(const QColor &inCircleColor);
  131.     //字符串形式设置数据
  132.     void setOutPieInfos(const QString &outPieInfos);
  133.     void setInPieInfos(const QString &inPieInfos);
  134.     //设置颜色集合
  135.     void setOutPieColors(const QList<QColor> &outPieColors);
  136.     void setInPieColors(const QList<QColor> &inPieColors);
  137.     //清空+设置饼图数据
  138.     void clearOutPie();
  139.     void clearInPie();
  140.     void appendOutPie(const RingData &data);
  141.     void appendInPie(const RingData &data);
  142. };
  143. #endif // CUSTOMRING_H

五、核心代码
  1. void CustomRing::paintEvent(QPaintEvent *)
  2. {
  3.     //绘制准备工作,启用反锯齿
  4.     QPainter painter(this);
  5.     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
  6.     int titleHeight = showTitle ? this->titleHeight : 0;
  7.     int legendHeight = showLegend ? this->legendHeight : 0;
  8.     QRect rect(0, titleHeight, this->width(), this->height() - titleHeight - legendHeight);
  9.     int side = qMin(rect.width(), rect.height());
  10.     qreal scale = side / 200.0;
  11.     //绘制背景
  12.     drawBg(&painter);
  13.     //平移坐标轴中心,等比例缩放
  14.     painter.save();
  15.     painter.translate(rect.center());
  16.     painter.scale(scale, scale);
  17.     //绘制外圆背景
  18.     drawOutCircle(&painter);
  19.     //绘制外层饼图
  20.     drawOutPie(&painter, scale, rect.center());
  21.     //绘制中间圆
  22.     drawMidCircle(&painter);
  23.     //绘制里层饼图
  24.     drawInPie(&painter, scale, rect.center());
  25.     //绘制里边圆
  26.     drawInCircle(&painter);
  27.     painter.restore();
  28.     //重新等比例缩放,绘制文字,文字放在后面绘制是为了不被圆遮挡
  29.     painter.scale(scale, scale);
  30.     //绘制标题
  31.     if (showTitle) {
  32.         drawTitle(&painter);
  33.     }
  34.     //绘制图例文字
  35.     if (showLegend) {
  36.         drawLegendText(&painter, scale);
  37.     }
  38. }
  39. void CustomRing::drawBg(QPainter *painter)
  40. {
  41.     painter->save();
  42.     painter->setPen(Qt::NoPen);
  43.     painter->setBrush(bgColor);
  44.     painter->drawRoundedRect(this->rect(), 5, 5);
  45.     painter->restore();
  46. }
  47. void CustomRing::drawOutCircle(QPainter *painter)
  48. {
  49.     int radius = 90;
  50.     painter->save();
  51.     painter->setPen(Qt::NoPen);
  52.     painter->setBrush(outCircleColor);
  53.     painter->drawEllipse(QPoint(0, 0), radius, radius);
  54.     painter->restore();
  55. }
  56. void CustomRing::drawMidCircle(QPainter *painter)
  57. {
  58.     int radius = 50;
  59.     painter->save();
  60.     painter->setPen(Qt::NoPen);
  61.     painter->setBrush(midCircleColor);
  62.     painter->drawEllipse(QPoint(0, 0), radius, radius);
  63.     painter->restore();
  64. }
  65. void CustomRing::drawInCircle(QPainter *painter)
  66. {
  67.     int radius = 10;
  68.     painter->save();
  69.     painter->setPen(Qt::NoPen);
  70.     painter->setBrush(inCircleColor);
  71.     painter->drawEllipse(QPoint(0, 0), radius, radius);
  72.     painter->restore();
  73. }



六、控件介绍
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/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发)QQ:517216493  WX:feiyangqingyun  QQ群:751439350
离线xdh873939316

只看该作者 6楼 发表于: 2019-08-10
回 liudianwu 的帖子
liudianwu:void CustomRing::mouseMoveEvent(QMouseEvent *event)
{
    //通过绘制路径的contains方法判断鼠标是否在对应区域内
....... (2019-08-09 15:38) 

非常感谢刘神。。上次买了你一个“树状导航栏”的控件,非常的实用
离线niyouhua

只看该作者 5楼 发表于: 2019-08-09
回 liudianwu 的帖子
liudianwu:void CustomRing::mouseMoveEvent(QMouseEvent *event)
{
    //通过绘制路径的contains方法判断鼠标是否在对应区域内
....... (2019-08-09 15:38) 

大佬能不能看下
drawOutPie(&painter, scale, rect.center());
drawInPie(&painter, scale, rect.center());
这二个函数的内容

离线liudianwu

只看该作者 4楼 发表于: 2019-08-09
xdh873939316:
大佬,可以开放一下 mouseMoveEvent 这个函数吗?


  1. void CustomRing::mouseMoveEvent(QMouseEvent *event)
  2. {
  3.     //通过绘制路径的contains方法判断鼠标是否在对应区域内
  4.     bool contains = false;
  5.     for (int i = 0; i < inPieInfo.size(); i++) {
  6.         if (inPieInfo.at(i).path.contains(event->pos())) {
  7.             inPieInfo[i].offset = 5;
  8.             contains = true;
  9.         } else {
  10.             if (inPieInfo.at(i).offset != 0) {
  11.                 contains = true;
  12.             }
  13.             inPieInfo[i].offset = 0;
  14.         }
  15.     }
  16.     //优先绘制内圆偏移
  17.     if (contains) {
  18.         for (int i = 0; i < outPieInfo.size(); i++) {
  19.             outPieInfo[i].offset = 0;
  20.         }
  21.         update();
  22.         return;
  23.     }
  24.     for (int i = 0; i < outPieInfo.size(); i++) {
  25.         if (outPieInfo.at(i).path.contains(event->pos())) {
  26.             outPieInfo[i].offset = 5;
  27.             contains = true;
  28.         } else {
  29.             if (outPieInfo.at(i).offset != 0) {
  30.                 contains = true;
  31.             }
  32.             outPieInfo[i].offset = 0;
  33.         }
  34.     }
  35.     if (contains) {
  36.         update();
  37.     }
  38. }


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

只看该作者 3楼 发表于: 2019-08-09
回 xdh873939316 的帖子
xdh873939316:大佬,可以开放一下 mouseMoveEvent 这个函数吗? (2019-08-08 20:27) 

这里面的代码是诱惑你买代码的部分,给你开放了,他还卖个毛,
离线xdh873939316

只看该作者 2楼 发表于: 2019-08-08
大佬,可以开放一下 mouseMoveEvent 这个函数吗?
离线niyouhua

只看该作者 1楼 发表于: 2019-08-05
本控件的难点并不是绘制环形或者饼图区域,初学者都会,难点在如何自动精准计算鼠标所在区域,然后高亮突出显示,难点代码没有放出来差评
快速回复
限100 字节
 
上一个 下一个