要使用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;
}