1.新建一个Widget项目,基类选“QWidget”
2.在pro文件中添加QT += charts
3.在界面上放置一个Widget,作为Chart的载体。右键点击这个widget,提升为:QtCharts::QChartView,头文件写:qchartview.h,勾选“全局包含”
4.widget.h添加:
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
QT_CHARTS_USE_NAMESPACE
5.widget.cpp的构造函数里添加:
QLineSeries *series = new QLineSeries();
series->append(0, 6);
series->append(2, 4);
*series << QPointF(3, 1) << QPointF(4, 3) ;
for(double x=5;x<10;x+=0.1)
{
series->append(x,sin(x));
}
QChart *chart = new QChart();
chart->addSeries(series);
//chart->legend()->hide();
//chart->createDefaultAxes();
//chart->setTitle("Simple line chart example");
ui->widget->setChart(chart);
//ui->widget->setRenderHint(QPainter::Antialiasing);
或者:
1.基类选QMainWindow;
2.不变
3.在界面上放置一个QGraphicsView,作为Chart的载体
4.mainwindow.h 添加
5.mainwindow.cpp 添加,最后改为:
ui->graphicsView->setChart(chart);
#include <QApplication>
#include <QChartView>
#include <QLineSeries>
#include <QPieSeries>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 图形视图
QGraphicsScene scene;
QGraphicsView view(&scene);
view.setWindowTitle("Simple chart");
view.setRenderHint(QPainter::Antialiasing);
scene.setBackgroundBrush(QBrush(QColor(240, 240, 240)));
view.setSceneRect(0, 0, 630, 280);
// 折线图
// 构建 series,作为折线图的数据源,为其添加 6 个坐标点
QLineSeries *lineseries = new QLineSeries();
lineseries->append(0, 5);
lineseries->append(4, 10);
lineseries->append(8, 6);
*lineseries << QPointF(13, 5) << QPointF(17, 6) << QPointF(20, 2);
QChart *lineChart = new QChart();
lineChart->legend()->hide(); // 隐藏图例
lineChart->addSeries(lineseries); // 将 series 添加至图表中
lineChart->createDefaultAxes(); // 基于已添加到图表的 series 来创建轴
lineChart->setTitle("Simple line chart");
lineChart->setGeometry(10, 10, 300, 260);
// 饼图
// 构建 series,作为饼图的数据源,为其添加 4 个切片
QPieSeries *pieSeries = new QPieSeries();
pieSeries->append("Java", 40);
pieSeries->append("C#", 30);
pieSeries->append("JS", 15);
QPieSlice *pieSlice = new QPieSlice("Others", 15);
pieSeries->append(pieSlice);
QChart *pieChart = new QChart();
pieChart->addSeries(pieSeries); // 将 series 添加至图表中
pieChart->legend()->setAlignment(Qt::AlignRight); // 设置图例靠右显示
pieChart->setTitle("Simple pie chart");
pieChart->setGeometry(320, 10, 300, 260);
// 将折线图、饼图添加至视图中
scene.addItem(lineChart);
scene.addItem(pieChart);
view.show();
return a.exec();
}
ref:http://blog.csdn.net/liang19890820/article/details/52210175
要链接到 Qt Charts 模块,需要在 qmake 项目文件中添加:
QT += charts
要构建图表,可以通过以下两种方式:
1.QChartView + QChart
QChartView 作为一个独立的图表窗口,用于显示 QChart,不需要与 QGraphicsScene 一起使用。
2.QGraphicsScene + QChart
在现有的 QGraphicsScene 中显示 QChart(或 QPolarChart)。
#include <QApplication>
#include <QChartView>
#include <QLineSeries>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 构建 series,作为图表的数据源,为其添加 6 个坐标点
QLineSeries *series = new QLineSeries();
series->append(0, 5);
series->append(4, 10);
series->append(8, 6);
*series << QPointF(13, 5) << QPointF(17, 6) << QPointF(20, 2);
// 构建图表
QChart *chart = new QChart();
chart->legend()->hide(); // 隐藏图例
chart->addSeries(series); // 将 series 添加至图表中
chart->createDefaultAxes(); // 基于已添加到图表的 series 来创轴
chart->setTitle("Simple line chart"); // 设置图表的标题
// 构建 QChartView,并设置抗锯齿、标题、大小
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
chartView->setWindowTitle("Simple line chart");
chartView->resize(400, 300);
chartView->show();
return a.exec();
}