QCustomPlot是C++ Qt数据可视化组件,可以绘制各种动态静态曲线,散点图,主状态等,功能和QCharts类。
官网:https://www.qcustomplot.com/index.php/introduction
最新的版本已经支持到Qt6.0,如下图 QCustomPlot转换成矢量图可以导出各种格式, 比如PDF文件和光栅图像像PNG, JPG, BMP。 在Qt应用程序时,QCustomPlot是实时数据显示比较好的解决方案。例如下面是QCustomPlot可以绘制的图形曲线:
下载QCustomPlot,解压,可以看到qcustomplot.h qcustomplot.cpp 以及示例程序,介绍文档,下面介绍在Qt中使用QCustomPlot:
(1) 新建一个GUI项目,拖动一个Widget控件到界面上,如果是手写布局,那么直接new QCustomPlot, 添加到布局即可。将Widget提升为QCustomPlot
(2) 在项目文件.pro中添加printsupport支持,如下图:
(3) 将qcustomplot.h qcustomplot.cpp导入到项目中来。在主项目头文件中添加头文件
#include "qcustomplot.h"
QCustomPlot提供了很多例子,大家可以根据需要,参照QCustomPlot提供的代码进行曲线绘制。例如我做的一个demo, 如下:
// configure plot to have two right axes:
ui->plot->yAxis->setTickLabels(false);
connect(ui->plot->yAxis2, SIGNAL(rangeChanged(QCPRange)), ui->plot->yAxis, SLOT(setRange(QCPRange))); // left axis only mirrors inner right axis
ui->plot->yAxis2->setVisible(true);
ui->plot->axisRect()->addAxis(QCPAxis::atRight);
ui->plot->axisRect()->axis(QCPAxis::atRight, 0)->setPadding(30); // add some padding to have space for tags
ui->plot->axisRect()->axis(QCPAxis::atRight, 1)->setPadding(30); // add some padding to have space for tags
// create graphs:
mGraph1 = ui->plot->addGraph(ui->plot->xAxis, ui->plot->axisRect()->axis(QCPAxis::atRight, 0));
mGraph2 = ui->plot->addGraph(ui->plot->xAxis, ui->plot->axisRect()->axis(QCPAxis::atRight, 1));
mGraph1->setPen(QPen(QColor(250, 120, 0)));
mGraph2->setPen(QPen(QColor(0, 180, 60)));
// create tags with newly introduced AxisTag class (see axistag.h/.cpp):
mTag1 = new AxisTag(mGraph1->valueAxis());
mTag1->setPen(mGraph1->pen());
mTag2 = new AxisTag(mGraph2->valueAxis());
mTag2->setPen(mGraph2->pen());
connect(&mDataTimer, SIGNAL(timeout()), this, SLOT(timerSlot()));
mDataTimer.start(40);
plot就是提升后的Widget控件,调用QCustomPlot的一些方法,就可以对坐标轴,曲线进行设置,再加上定时器等,就能完成一个动态曲线。运行界面
本项目源码: https://codechina.csdn.net/yao_hou/qcustomplot_test