QChart draws static graphs

QChart

Starting from QT5.7, the community version also includes Qt Charts. QtCharts can easily draw common line charts, bar charts, pie charts, etc.

  • QtCharts is based on QT's Graphics View architecture. Its core components are: QChartView, QChart. To use QChart, you must install QtCharts components when installing QT.

QChart inheritance relationship

QGraphicsItem
QGraphicObject
QGraphicWidget
QChart
QPolarQchart

QChart use process

Add header file

Add Qt += charts in the .pro file and add
in the header file of the class:

#include
using namespace QtCharts;

or

#include
QT_CHARTS_USE_NAMESPACE

Create QChartView
  • Mainly use GraphicView to upgrade to QChartView.
  • Note: This step should be done after adding charts in the pro folder
Created with Raphaël 2.2.0 开始 在ui中的创建GraphicsView 右键GraphicsView,选择promote to -> QChartView 结束
Simple drawing
Created with Raphaël 2.2.0 开始 添加QChartView 构建QChart对象 使用setChart为QChartView绑定QChart 创建序列,并添加序列值 创建坐标轴,并将坐标轴和序列添加到QChart中 结束
Example code

Create a new MainWindow project, add QChartView in ui, omit the object name chartView
main.c, mainwindows.h is the default, and the constructor of mainwindows.c is as follows:

MainWindow::MainWindow(QWidget *parent):QMainWindow(parent),ui(new Ui::MainWindow)
{
    
    
	ui->setupUi(this);
	QChart *chart = new QChart;
	ui->chartView->setChart(chart);//绑定的接口
	chart->setTitle("sample"); //设置图名称
	QLineSeries *serie0 = new QLineSeries(chart);//设置父对象,则自动析构
	series0->setName("sin");
	series0->setUseOpenGL(true);
	chart->addSeries(series0);
	
	//修改序列中值
	for(double i= 0; i<10;i+=0.1){
    
    
		series0.append(i,qSin(i));
	}
	//添加坐标轴,一对坐标轴即可
	QValueAxis *axisX = new QValueAxis(chart);
	QValueAxis *axisY = new QVlaueAxis(chart);	
	axisX->setRange(0,10);
	axisY->setRange(-2,2);
	//添加序列至图中
	chart->setAxisX(axisX,series0);
	chart->setAxisY(axisY,series0);
}
QLineSeries and QscatterSeries use OpenGL acceleration

These two types of sequences can be accelerated using OpenGL. If OpenGL acceleration is used, a layer of QOpenGLWidget displayed on the QChartView will be added, and the accelerated sequence will be drawn on the QOpenGLWidget.

series->setUseOpenGL(true);

Guess you like

Origin blog.csdn.net/u013894391/article/details/101424470