创建折线图
注意:这是 “带小部件的图表”示例库的一部分。

要创建折线图,需要一个 QLineSeries 实例。让我们创建一个。
auto series = new QLineSeries;然后,我们将数据添加到该系列中。我们可以使用 append() 成员函数,也可以使用流运算符。
series->append(0, 6);
series->append(2, 4);
series->append(3, 8);
series->append(7, 4);
series->append(10, 5);
*series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);要在图表上显示数据,我们需要一个 QChart 实例。我们将系列添加到该实例中,创建默认坐标轴,并设置图表标题。
auto chart = new QChart;
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("Simple Line Chart");接着,我们创建一个以 QChart 为参数的 QChartView 对象。这样,我们就无需自己创建QGraphicsView 场景了。
createDefaultChartView(chart);图表现已准备就绪,可以显示了。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.