本示例介绍如何绘制动态数据并显示出来。
使用QSplineSeries+定时器刷新数据实现。
Chart *chart = new Chart;
chart->setTitle("Dynamic spline chart");
QChartView chartView(chart);m_series = new QSplineSeries(this);
QPen green(Qt::red);
green.setWidth(3);
m_series->setPen(green);
m_series->append(m_x, m_y);QObject::connect(&m_timer, &QTimer::timeout, this, &Chart::handleTimeout);
m_timer.setInterval(1000);void Chart::handleTimeout()
{
qreal x = plotArea().width() / m_axisX->tickCount();
qreal y = (m_axisX->max() - m_axisX->min()) / m_axisX->tickCount();
m_x += y;
m_y = QRandomGenerator::global()->bounded(5) - 2.5;
m_series->append(m_x, m_y);
scroll(x, 0);
if (m_x == 100)
m_timer.stop();
}C:\Qt\{你的Qt版本}\Examples\{你的Qt版本}\charts\dynamicsplinehttps://doc.qt.io/qt-5/qtcharts-dynamicspline-example.html