Table of contents
-
- Create a new project & open the official sample project
- Step1 Link the charts module (you must choose to install the Qt Charts module when installing Qt)
- Step2 Look at the official example to prepare for the work (import the use of the required library namespace macro definition)
- Step3 Provide the carrier
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
. It will automatically pop up to the help page and return to the qt creator interface
. 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 figure
Step1 Link the charts module (you must choose to install the Qt Charts module when installing Qt)
Add charts as shown in the figure
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
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
Step3 Provide the carrier
Open the mainwindow.ui file, use the Graphics View control
to upgrade to
QChartView as shown in the figure, the following qchartview.h will appear automatically, click Add
and click Upgrade
, you can see that the class of the control object graphicsView just added has become QChartView
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
to the official example main.cpp
-> 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
Tips, select ctrl+i to quickly typeset
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.
Similarly, modify the second code that indicates setting anti-aliasing.
Click the green play button to build and run
successfully . !
This is the end, if you still want to know the specific meaning of these codes, read the documentation