Qt graphics image development curve chart module QChart library use

How to compile and install QChart, please check the following article

Qt Graphics and Image Development Curve Chart Library QtChart Compilation and Installation Detailed Methods and Use Examples

When using the Qt curve chart module Chart library, you must first pay attention to 3 points:

(1) Add in the .pro file: QT += charts.

(2) Add to the QChart file: QT_CHARTS_USE_NAMESPACE, or: using namespace QtCharts;

Drag a graphicsView control into the ui interface, then right-click to promote it to the QChartView class, write the promoted class: QtCharts::QChartView, and write the header file: qchartview.h

(3) Don't forget to add #include <QChartView> to the file that uses QChartView

If an error is reported during compilation, please execute in sequence: clear -> execute qmake -> build

Two display methods of QChart: (essentially the same method)

(1) QChart must be displayed on the widget, more precisely, it must be displayed in the QGraphicView control. As we all know, QT's ui controls can be directly displayed as an independent window, so this is method 1.

(2) Display QChart in the QGraphicView control of the ui interface.

PS: To display QChart in the QGraphicView control, the program is a bit cumbersome. QT has already encapsulated a ui control class QtCharts::QChartView for us, which inherits QGraphicView. This is the promotion of QGraphicView to QChartView mentioned above. It is very convenient to use QChartView to display QChart.

It is cumbersome to use QGraphicView to display QChart. Let’s compare it. Use QGraphicView and QChartView to display QChart respectively. The difference in the amount of code:

(1) To display with QGraphicView, you need to use the QGraphicsScene class. Specifically, QGraphicView is at the bottom, QGraphicsScene is in the middle, and QChart is at the outermost layer.

QGraphicsScene scene;//场景(中间层)
QGraphicsView view(&scene);//视图(最底层)
view.setRenderHint(QPainter::Antialiasing);//设置视图抗锯齿
view.setSceneRect(0, 0, 630, 280);//设置视图大小
 
QLineSeries *lineseries = new QLineSeries();//图表的数据集
lineseries->append(0, 5);//append和<<功能差不多
*lineseries << QPointF(13, 5) << QPointF(17, 6) << QPointF(20, 2);
 
QChart *lineChart = new QChart();//图表(最顶层)
lineChart->addSeries(lineseries); // 将 数据集 添加至图表中
 
scene.addItem(lineChart);//把图标添加到场景中(一个场景中允许添加多个图表)
view.show();//视图显示

(2) It is more convenient to display QChart with QChartView

QLineSeries *lineseries = new QLineSeries();//图表的数据集
lineseries->append(0, 5);//append和<<功能差不多
*lineseries << QPointF(13, 5) << QPointF(17, 6) << QPointF(20, 2);
 
QChart *lineChart = new QChart();//图表(最顶层)
lineChart->addSeries(lineseries); // 将 数据集 添加至图表中
 
QChartView *chartView = new QChartView(chart);//QChartView 可以一步到位直接显示QChart
chartView->setRenderHint(QPainter::Antialiasing);//继承来的抗锯齿方法
chartView->resize(400, 300);
chartView->show();//本示例代码,把该控件作为窗口直接显示出来。
//其实,也可以把QChartView的父控件设置为主UI,这样就能把QChartView显示在主窗体中了

Finally, let's take a look at the hierarchical relationship between the various classes. In the figure below, curly brackets "{" represent that the left entity contains the right entity, and the down arrow ↓↓↓↓ represents the inheritance relationship

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓ 

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/130688815