使用QChart写一个QT动态温度曲线
时间: 2024-06-09 19:04:32 浏览: 179
下面是一个简单的示例代码,使用QChart和QChartView绘制动态温度曲线:
```cpp
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCore/QRandomGenerator>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow window;
QChartView *chartView = new QChartView(&window);
window.setCentralWidget(chartView);
QChart *chart = new QChart();
chart->setTitle("Temperature Curve");
QLineSeries *series = new QLineSeries();
chart->addSeries(series);
chart->createDefaultAxes();
chart->axisX()->setTitleText("Time");
chart->axisY()->setTitleText("Temperature");
chartView->setChart(chart);
window.show();
QRandomGenerator gen;
qreal x = 0;
qreal y = gen.bounded(0, 100);
while (true) {
series->append(x, y);
x += 1;
y = gen.bounded(0, 100);
chart->axisX()->setRange(x, 10, Qt::AlignRight);
chartView->update();
QApplication::processEvents();
QThread::msleep(50);
}
return a.exec();
}
```
这个示例代码会打开一个窗口,绘制一个名为“Temperature Curve”的图表,随着时间的推移,将动态添加温度数据点。
阅读全文