[Details] Put the qtchart chart on the ui, take the example of putting the official line chart on the ui interface as an example


Foreword: I just started to get in touch with Qt, and I faced various pitfalls when I read the blog and official documents of the big brother, and it was very uncomfortable.
Therefore, posted a record to ensure that all details are complete and friendly to Xiaobai

The use environment is Qt5.14.0 MinGW 64

Create a new project & open the official sample project

Search for chart, click to open the sample program of the line chart
insert image description here
. It will automatically pop up to the help page and return to the qt creator interface insert image description here
. If the opening fails, the project becomes an exclamation mark and there is only one project file. You can refer to my other blog

Create a new project, select as shown in the figureinsert image description here

Step1 Link the charts module (you must choose to install the Qt Charts module when installing Qt)


insert image description here
Add charts as shown in the figure
insert image description here

Step2 Look at the official example to prepare for the work (import the use of the required library namespace macro definition)

Check the main.cpp of the official example and find the two required libraries, a macro definition of a namespace
insert image description here
and add it to the mainwindow.cpp of our project. Be careful to put it before ui_mainwindow.h, otherwise an error will be reported in the ui_mainwindow.h file
insert image description here

insert image description here

Step3 Provide the carrier

Open the mainwindow.ui file, use the Graphics View control
insert image description here
insert image description here
to upgrade to
insert image description here
QChartView as shown in the figure, the following qchartview.h will appear automatically, click Add
insert image description here
and click Upgrade
insert image description here
, you can see that the class of the control object graphicsView just added has become QChartView
insert image description here

Go back to the program interface and click the green play button to build and run it once.
This wave is for the next time when typing the code, there will be the object just added, otherwise there will be a strange error and no code prompt. Copy code 1
insert image description here
to the official example main.cpp
insert image description here
-> 4 are related codes, 5 we have our own in the project, so we don’t need to
put it in our mainwindow.cpp, the location is shown in the picture
insert image description here
insert image description here
Tips, select ctrl+i to quickly typeset
insert image description here
and modify the code in 4, and put the icon in ps in our graphicsView object
: if you haven’t built it first, you won’t be able to find the graphicsView in the quick prompt as shown in the picture, and you will get an error if you type it in.
insert image description here
insert image description here
insert image description here
Similarly, modify the second code that indicates setting anti-aliasing.
insert image description here
Click the green play button to build and run
insert image description here
successfully . !

This is the end, if you still want to know the specific meaning of these codes, read the documentation

Guess you like

Origin blog.csdn.net/qq_47110957/article/details/119513401