• 一个实时波形图的封装demo(QT)(qcustomplot)


    前言:

            封装的一个实时波形图的类,可以直接提升使用。 提供了接口,可以更改颜色,样式,等等

    参考:

    Qt Plotting Widget QCustomPlot - Introduction

    另外参考了一个大神的作品,链接没找到。

    项目文件:

    123盘  

    123盘  实时波形图官方版下载丨最新版下载丨绿色版下载丨APP下载-123云盘

    CSDN

    【免费】一个实时波形图的封装demo(QT)(qcustomplot)资源-CSDN文库

    源码

    WaveChart文件夹

    1.加入文件qcustomplot.h 和 qcustomplot.cpp:

    2.WaveChart.pri文件:

    1. QT += core gui
    2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
    3. FORMS += \
    4. $$PWD/waveWidget.ui
    5. HEADERS += \
    6. $$PWD/qcustomplot.h \
    7. $$PWD/waveWidget.h
    8. SOURCES += \
    9. $$PWD/qcustomplot.cpp \
    10. $$PWD/waveWidget.cpp

    3.waveWidget.h文件:

    1. #ifndef WAVEWIDGET_H
    2. #define WAVEWIDGET_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. //#include
    9. #include "qcustomplot.h"
    10. using std::vector;
    11. namespace Ui {
    12. class WaveWidget;
    13. }
    14. class WaveWidget : public QWidget
    15. {
    16. Q_OBJECT
    17. public:
    18. explicit WaveWidget(QWidget *parent = nullptr);
    19. ~WaveWidget();
    20. void init(bool isShowTicks=true, bool isShowTickLables=true, bool isShowGrid=true, bool isShowSubGrid=true, bool isShowAxis2=true, bool isGraphBigAndSmall=true);
    21. void setTheme(QColor axis, QColor background);
    22. void setXAxisLable(const char* name, const char* name2 = "");
    23. void setYAxisLable(const char* name, const char* name2 = "");
    24. void setPen(Qt::GlobalColor penColor, QCPGraph::LineStyle lineStyle = QCPGraph::lsLine, QColor brushColor = QColor(255,255, 255, 100), bool isShow = true);
    25. void addNewData(double data);
    26. void setTitle(const char* name);
    27. double getMax();
    28. double getMin();
    29. double getAverage();
    30. QCustomPlot* getTheChartPlot();
    31. QCPGraph* getTheGraph();
    32. vector<double> &getTheVector();
    33. private:
    34. Ui::WaveWidget *ui;
    35. QCustomPlot* chartPlot = new QCustomPlot(this); // 图表
    36. QCPGraph* graph; // 曲线
    37. //QScrollBar horizontalScrollBar;
    38. vector<double> graphData; // 图表数据,只作记录使用
    39. int timeRange = 10; // 时间轴范围
    40. QTime time;
    41. double sum = 0.0;
    42. };
    43. #endif // WAVEWIDGET_H

    4.waveWidget.cpp文件

    1. #include "waveWidget.h"
    2. #include "ui_waveWidget.h"
    3. WaveWidget::WaveWidget(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::WaveWidget)
    6. {
    7. ui->setupUi(this);
    8. // 初始化图表
    9. graph = chartPlot->addGraph();
    10. // graph->addData(1,1);
    11. // graph->addData(2,3);
    12. // 初始化设置
    13. // chartPlot->xAxis->setRange(1, 4096);
    14. // chartPlot->yAxis->setRange(-1, 350); // 假设灰度值的范围为0-255
    15. // chartPlot->xAxis->setLabel("Pixel");
    16. // chartPlot->yAxis->setLabel("Gray Value");
    17. QVBoxLayout* layout = new QVBoxLayout;
    18. layout->addWidget(ui->title);
    19. layout->addWidget(chartPlot);
    20. chartPlot->setSizePolicy(QSizePolicy::Policy(QSizePolicy::Preferred), QSizePolicy::Policy(QSizePolicy::Expanding));
    21. //layout->addWidget((QWidget)horizontalScrollBar);
    22. setLayout(layout);
    23. // init();
    24. }
    25. WaveWidget::~WaveWidget()
    26. {
    27. delete ui;
    28. }
    29. void WaveWidget::init(bool isShowTicks, bool isShowTickLables, bool isShowGrid, bool isShowSubGrid, bool isShowAxis2, bool isGraphBigAndSmall)
    30. {
    31. time = QTime::currentTime();
    32. // 刻度显示
    33. chartPlot->xAxis->setTicks(isShowTicks);
    34. chartPlot->yAxis->setTicks(isShowTicks);
    35. // 刻度值显示
    36. chartPlot->xAxis->setTickLabels(isShowTickLables);
    37. chartPlot->yAxis->setTickLabels(isShowTickLables);
    38. // 网格显示
    39. chartPlot->xAxis->grid()->setVisible(isShowGrid);
    40. chartPlot->yAxis->grid()->setVisible(isShowGrid);
    41. // 子网格显示
    42. chartPlot->xAxis->grid()->setSubGridVisible(isShowSubGrid);
    43. chartPlot->yAxis->grid()->setSubGridVisible(isShowSubGrid);
    44. // 右和上坐标轴、刻度值显示
    45. chartPlot->xAxis2->setVisible(isShowAxis2);
    46. chartPlot->yAxis2->setVisible(isShowAxis2);
    47. chartPlot->yAxis2->setTicks(isShowAxis2);
    48. chartPlot->yAxis2->setTickLabels(isShowAxis2);
    49. // make top right axes clones of bottom left axes. Looks prettier:
    50. // chartPlot->axisRect()->setupFullAxesBox();
    51. // make left and bottom axes always transfer their ranges to right and top axes:
    52. if(isShowAxis2){
    53. connect(chartPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), chartPlot->xAxis2, SLOT(setRange(QCPRange)));
    54. connect(chartPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), chartPlot->yAxis2, SLOT(setRange(QCPRange)));
    55. }
    56. // 暗色主题
    57. //setPlotTheme(Qt::white, Qt::black);
    58. // 亮色主题
    59. setTheme(Qt::black, Qt::white);
    60. if(isGraphBigAndSmall){
    61. // 可放大缩小和移动
    62. chartPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    63. }
    64. // x轴以时间形式显示
    65. QSharedPointer timeTicker(new QCPAxisTickerTime);
    66. timeTicker->setTimeFormat("%h:%m:%s");
    67. chartPlot->xAxis->setTicker(timeTicker);
    68. chartPlot->axisRect()->setupFullAxesBox();
    69. chartPlot->replot();
    70. }
    71. void WaveWidget::setTheme(QColor axis, QColor background)
    72. {
    73. //----------------------------------------------------------------------------------------//
    74. // 坐标标注颜色
    75. chartPlot->xAxis->setLabelColor(axis);
    76. chartPlot->yAxis->setLabelColor(axis);
    77. // 坐标刻度值颜色
    78. chartPlot->xAxis->setTickLabelColor(axis);
    79. chartPlot->yAxis->setTickLabelColor(axis);
    80. // 坐标基线颜色和宽度
    81. chartPlot->xAxis->setBasePen(QPen(axis, 1));
    82. chartPlot->yAxis->setBasePen(QPen(axis, 1));
    83. // 坐标主刻度颜色和宽度
    84. chartPlot->xAxis->setTickPen(QPen(axis, 1));
    85. chartPlot->yAxis->setTickPen(QPen(axis, 1));
    86. // 坐标子刻度颜色和宽度
    87. chartPlot->xAxis->setSubTickPen(QPen(axis, 1));
    88. chartPlot->yAxis->setSubTickPen(QPen(axis, 1));
    89. // 坐标标注颜色
    90. chartPlot->xAxis2->setLabelColor(axis);
    91. chartPlot->yAxis2->setLabelColor(axis);
    92. // 坐标刻度值颜色
    93. chartPlot->xAxis2->setTickLabelColor(axis);
    94. chartPlot->yAxis2->setTickLabelColor(axis);
    95. // 坐标基线颜色和宽度
    96. chartPlot->xAxis2->setBasePen(QPen(axis, 1));
    97. chartPlot->yAxis2->setBasePen(QPen(axis, 1));
    98. // 坐标主刻度颜色和宽度
    99. chartPlot->xAxis2->setTickPen(QPen(axis, 1));
    100. chartPlot->yAxis2->setTickPen(QPen(axis, 1));
    101. // 坐标子刻度颜色和宽度
    102. chartPlot->xAxis2->setSubTickPen(QPen(axis, 1));
    103. chartPlot->yAxis2->setSubTickPen(QPen(axis, 1));
    104. // 整个画布背景色
    105. chartPlot->setBackground(background);
    106. // 绘图区域背景色
    107. chartPlot->axisRect()->setBackground(background);
    108. // 刷新绘图
    109. chartPlot->replot();
    110. }
    111. void WaveWidget::setXAxisLable(const char *name, const char* name2)
    112. {
    113. chartPlot->xAxis->setLabel(name);
    114. if(strlen(name2)!=0)
    115. chartPlot->xAxis2->setLabel(name2);
    116. chartPlot->replot();
    117. }
    118. void WaveWidget::setYAxisLable(const char *name, const char* name2)
    119. {
    120. chartPlot->yAxis->setLabel(name);
    121. if(strlen(name2)!=0)
    122. chartPlot->yAxis2->setLabel(name2);
    123. chartPlot->replot();
    124. }
    125. void WaveWidget::setPen(Qt::GlobalColor penColor, QCPGraph::LineStyle lineStyle, QColor brushColor, bool isShow)
    126. {
    127. graph->setPen(QPen(penColor)); // 设置线条颜色为蓝色
    128. graph->setLineStyle(lineStyle); // 设置线型为直线
    129. graph->setVisible(isShow);
    130. chartPlot->graph()->setBrush(QBrush(brushColor));
    131. chartPlot->replot();
    132. }
    133. void WaveWidget::addNewData(double data)
    134. {
    135. // 系统当前时间 = 系统运行初始时间 + 系统运行时间
    136. static double start = time.hour()*60*60 + time.minute()*60 + time.second() + time.msec()/1000.0;
    137. double key = start + time.elapsed()/1000.0;
    138. // 设置时间轴
    139. chartPlot->xAxis->setRange(key, timeRange, Qt::AlignRight);
    140. //chartPlot->rescaleAxes();
    141. // 刷新绘图水平滚动条
    142. //horizontalScrollBar.setRange(int(start), int(key)); // 刷新滚动条的范围
    143. //horizontalScrollBar.setPageStep(1); // 设置翻页步长为 1s 的宽度
    144. //horizontalScrollBar.setValue(int(key)); // 调整滑块位置到最右边
    145. // 更新曲线绘图
    146. chartPlot->graph()->addData(key, data);
    147. chartPlot->replot(QCustomPlot::rpQueuedReplot);
    148. // 存储曲线的当前值
    149. graphData.push_back(data);
    150. sum += data;
    151. }
    152. void WaveWidget::setTitle(const char *name)
    153. {
    154. ui->title->setText(name);
    155. }
    156. double WaveWidget::getMax()
    157. {
    158. return *std::max_element(graphData.begin(),graphData.end());
    159. }
    160. double WaveWidget::getMin()
    161. {
    162. return *std::min_element(graphData.begin(),graphData.end());
    163. }
    164. double WaveWidget::getAverage()
    165. {
    166. return 1.0*sum/(graphData.size());
    167. }
    168. QCustomPlot *WaveWidget::getTheChartPlot()
    169. {
    170. return chartPlot;
    171. }
    172. QCPGraph *WaveWidget::getTheGraph()
    173. {
    174. return graph;
    175. }
    176. vector<double> &WaveWidget::getTheVector()
    177. {
    178. return graphData;
    179. }

    5.测试demo:

    新建测试界面:

    提升为刚刚的类:

    效果:

    测试:

    1. ui->setupUi(this);
    2. // 初始化:可以设置参数
    3. ui->waveWidget->init();
    4. // 可选设置:
    5. ui->waveWidget->setXAxisLable("Time");
    6. ui->waveWidget->setYAxisLable("Value");
    7. ui->waveWidget->setPen(Qt::blue, QCPGraph::lsLine, QColor( 36,141,218,255));
    8. ui->waveWidget->setTheme(Qt::red, Qt::white);
    9. ui->waveWidget->setTitle("Wave Chart Test");
    10. // 测试加入数据:
    11. QTimer *timer = new QTimer(this);
    12. connect(timer,&QTimer::timeout,[=](){
    13. ui->waveWidget->addNewData((qrand()%1000)/1000.0);
    14. qDebug()<<"max:"<waveWidget->getMax()<<" min:"<waveWidget->getMin()<<" average:"<waveWidget->getAverage();
    15. if(num%10==0){
    16. qDebug()<<"size: "<waveWidget->getTheVector().size();
    17. ui->waveWidget->getTheVector().clear();
    18. qDebug()<<"size: "<waveWidget->getTheVector().size()<<"********";
    19. }
    20. if(num>50){
    21. timer->stop();
    22. }
    23. num++;
    24. });
    25. timer->start(100);

  • 相关阅读:
    TOWER 成就徽章 NFT 系列介绍——TOWER 生态系统的第一个灵魂通证(SBT)
    AI绘图有哪些高效路径?
    分类问题经典算法 | 多分类问题 | Softmax回归:梯度下降
    【前端JS】ES6的几种新特性
    数据库1~4NF+ BCNF
    3.1 OrCAD中怎么创建新的原理图工程文件?OrCAD中原理图的设计纸张大小应该怎么设置?
    (附源码)基于spring boot 房屋租赁系统小程序-计算机毕设 88306
    kafka广播消费组停机后未删除优化
    前端技术栈学习:Vue2、Vue cli脚手架、ElementUI组件库、Axios
    预警就是踩刹车,六步法定义预警体系
  • 原文地址:https://blog.csdn.net/Alon1787/article/details/136345749