Qt中可以使用QCustomPlot绘制实时温度曲线图。首先,需要在Qt中安装QCustomPlot插件,然后在代码中添加头文件,创建QCustomPlot对象,并设置坐标轴。接着,通过在定时器中获取新的温度数据,并使用addData()函数将其添加到曲线图中,实现实时绘制。最后,通过replot()函数刷新图形。代码示例如下:
#include <QtWidgets>
#include "qcustomplot.h"
int main(int argc, char *argv[])
QApplication a(argc, argv);
QCustomPlot *customPlot = new QCustomPlot;
customPlot->addGraph();
customPlot->graph(0)->setPen(QPen(Qt::blue));
customPlot->xAxis->setLabel("x");
customPlot->yAxis->setLabel("y");
customPlot->xAxis->setRange(0, 10);
customPlot->yAxis->setRange(0, 100);
customPlot->replot();
customPlot->show();
QTimer dataTimer;
QObject::connect(&dataTimer, &QTimer::timeout, [customPlot](){
static double x = 0;
double y = qSin(x) + qrand() / (double)RAND_MAX * 0.5;
customPlot->graph(0)->addData(x, y);
customPlot->xAxis->setRange(x, 10, Qt::AlignRight);
customPlot->replot();
x += 0.1;
dataTimer.start(100);
return a.exec();
mahuifa