• 3550阅读
  • 0回复

Qt编写自定义控件57-直方波形图 [复制链接]

上一主题 下一主题
离线liudianwu
 

只看楼主 倒序阅读 楼主  发表于: 2019-09-22
一、前言
直方波形图控件非原创控件,控件大全中大概有20-30个控件非自己原创,而是参考了网上开源的代码,自己加以整理和完善,新增了插件的代码使得可以直接集成到QtDesigner或者QtCreator中。直方波形图,主要就是将外部传入的坐标集合数据进行渐变过渡的绘制,产生一个动态的过渡效果,将设置的坐标集合重新运算+1变成新的坐标集合来绘制,这样看起来绘制不会很死,而是缓慢的过渡。

二、实现的功能
* 1:可设置最大值
* 2:可设置每次过渡的步长
* 3:可设置item之间的间隔
* 4:可设置渐变的背景颜色
* 5:可设置线条的颜色

三、效果图



四、头文件代码
  1. #ifndef WAVELINE_H
  2. #define WAVELINE_H
  3. /**
  4. * 直方波形图控件 作者:feiyangqingyun(QQ:517216493) 2016-11-6
  5. * 1:可设置最大值
  6. * 2:可设置每次过渡的步长
  7. * 3:可设置item之间的间隔
  8. * 4:可设置渐变的背景颜色
  9. * 5:可设置线条的颜色
  10. */
  11. #include <QWidget>
  12. #ifdef quc
  13. #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
  14. #include <QtDesigner/QDesignerExportWidget>
  15. #else
  16. #include <QtUiPlugin/QDesignerExportWidget>
  17. #endif
  18. class QDESIGNER_WIDGET_EXPORT WaveLine : public QWidget
  19. #else
  20. class WaveLine : public QWidget
  21. #endif
  22. {
  23.     Q_OBJECT
  24.     Q_PROPERTY(int maxValue READ getMaxValue WRITE setMaxValue)
  25.     Q_PROPERTY(int step READ getStep WRITE setStep)
  26.     Q_PROPERTY(int space READ getSpace WRITE setSpace)
  27.     Q_PROPERTY(QColor bgColorStart READ getBgColorStart WRITE setBgColorStart)
  28.     Q_PROPERTY(QColor bgColorEnd READ getBgColorEnd WRITE setBgColorEnd)
  29.     Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor)
  30. public:
  31.     explicit WaveLine(QWidget *parent = 0);
  32.     ~WaveLine();
  33. protected:
  34.     void paintEvent(QPaintEvent *);
  35.     void drawBg(QPainter *painter);
  36.     void drawLine(QPainter *painter);
  37. private:
  38.     int maxValue;                   //最大值
  39.     int step;                       //步长
  40.     int space;                      //间距
  41.     QColor bgColorStart;            //背景渐变开始颜色
  42.     QColor bgColorEnd;              //背景渐变结束颜色
  43.     QColor lineColor;               //线条颜色
  44.     QTimer *timer;                  //绘制定时器
  45.     QVector<int> currentDataVec;    //当前数据集合
  46.     QVector<int> dataVec;           //目标数据集合
  47. private slots:
  48.     void updateData();
  49. public:
  50.     int getMaxValue()               const;
  51.     int getStep()                   const;
  52.     int getSpace()                  const;
  53.     QColor getBgColorStart()        const;
  54.     QColor getBgColorEnd()          const;
  55.     QColor getLineColor()           const;
  56.     QSize sizeHint()                const;
  57.     QSize minimumSizeHint()         const;
  58. public Q_SLOTS:
  59.     //设置数据
  60.     void setData(const QVector<int> &dataVec);
  61.     //设置最大值
  62.     void setMaxValue(int maxValue);
  63.     //设置步长
  64.     void setStep(int step);
  65.     //设置间距
  66.     void setSpace(int space);
  67.     //设置背景颜色
  68.     void setBgColorStart(const QColor &bgColorStart);
  69.     void setBgColorEnd(const QColor &bgColorEnd);
  70.     //设置线条颜色
  71.     void setLineColor(const QColor &lineColor);
  72. };
  73. #endif // WAVELINE_H

五、核心代码
  1. void WaveLine::paintEvent(QPaintEvent *)
  2. {
  3.     //绘制准备工作,启用反锯齿
  4.     QPainter painter(this);
  5.     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
  6.     //绘制背景
  7.     drawBg(&painter);
  8.     //绘制线条
  9.     drawLine(&painter);
  10. }
  11. void WaveLine::drawBg(QPainter *painter)
  12. {
  13.     painter->save();
  14.     painter->setPen(Qt::NoPen);
  15.     QLinearGradient bgGradient(QPoint(0, 0), QPoint(0, height()));
  16.     bgGradient.setColorAt(0.0, bgColorStart);
  17.     bgGradient.setColorAt(1.0, bgColorEnd);
  18.     painter->setBrush(bgGradient);
  19.     painter->drawRect(rect());
  20.     painter->restore();
  21. }
  22. void WaveLine::drawLine(QPainter *painter)
  23. {
  24.     painter->save();
  25.     painter->setPen(QPen(lineColor, 2));
  26.     int count = dataVec.count();
  27.     double increment = (double)width() / count;
  28.     double initX = 0;
  29.     QVector<QPointF> pointVec;
  30.     for (int i = 0; i < count - 1; i++) {
  31.         double currentValue = currentDataVec.at(i);
  32.         double y1 = height() - (double)height() / maxValue * currentValue;
  33.         double nextValue = currentDataVec.at(i + 1);
  34.         double y2 = height() - (double)height() / maxValue * nextValue;
  35.         QPointF point1(initX, y1);
  36.         QPointF point2(initX + increment, y2);
  37.         initX += increment;
  38.         pointVec.append(point1);
  39.         pointVec.append(point2);
  40.     }
  41.     painter->drawLines(pointVec);
  42.     painter->restore();
  43. }

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