要使用QtCharts 需要几个步骤:
1. pro 文件中 修改
QT += charts
2. 把 chart 相关的头文件放在 ui 相关的头文件上面,否则会导致 编译错误,找不到 chartView 相关。
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
QT_CHARTS_USE_NAMESPACE
3. 在 ui 中放入 widget,然后右键 widget,选择 promote to 选项,选项卡里面:
base class name: QWidget
promoted class name: QChartView
Header file: QChartView
4. 注意 setChart 之后需要把 旧的给删除掉。否则内存占用越来越多。
QChart *oldChart = ui->widget->chart();
QChart *chart = new QChart();
ui->widget->setChart(chart);
if (oldChart != nullptr) {
delete oldChart;
}