My QT5 Learning Path (ii)--The first program (Hello World)

Source: Internet
Author: User

First, preface

"工欲善其事, its prerequisite", the previous section, I introduced the QT installation and configuration method, set up a basic development platform. This section, to learn about QT's programming styles and specifications, begins with a simple example.

Second, the first procedure of--hello World

First of all, we can build a new project according to the method of the previous section, the name of the project can be called Hello, with you. In the process of creating the project, there is a choice whether to create a view interface options, this can not be selected, because we now just understand the mechanism of QT, do not need QT to help us do too much, after the creation is completed, open main.cpp.

The main work I do is:

1, block out the program's own dialog box program code;

2, add a Label control, and give him a text value, the last display.

The result of the final display:

Second, let's analyze the basic process of QT. The line is the header file contains, there are two kinds of header files, the first is a custom header file or a local file, with "" to represent and contain, the second is the system header file, here is the QT comes with the header file, directly with <> to represent and include it. The 7th line is to create an instance of Qapplication, for the Qt program, the main() function is generally created Application object ( QApplication GUI program is, non-GUI program is QCoreApplication . QApplicationis actually a QCoreApplication subclass. This object is used to manage the life cycle of the QT program and turn on the event loop. 10~11 line is the core code, that is, we actually add the use case code, here I created a qlabel, using the constructor to assign the operation, and finally called the Show method to display it. The last line calls Exec, which turns on the event loop (which can be understood as a wireless loop).

After writing these two code, we think of a problem, here we first do not discuss the message mechanism of QT and other communication principles, purely from the C + + aspects of the stability and robustness of the program.

Question 1: Do I create a qlabel on a stack or on a heap?

2: What problems should I pay attention to if I create the Qlabel variable as a variable on the heap?

Let's start with question 1, which should be uncontroversial, and the Qlabel variable is created on the stack. Take another look at question 2, if I declare a variable as a variable on the heap because of some requirement, then I will allocate space for this variable. This time the problem comes, allocate space, who will release after the program is over? What if the memory leaks? How to prevent memory leaks ah? If we ignore it, the operating system will recycle it after the program finishes, but we see that the label is built on the heap and the app is built on the stack. This means that the label will be refactored after the app. In other words, the label's life cycle is longer than the app's life cycle. This is a big taboo for Qt programming . Because in Qt, all QPaintDevice must be QApplication created and used in cases where there are instances. If you are curious, you can mention the QLabel subclass that inherits from it QWidget QWidget QPaintDevice . There is no problem with the code above, because when the app exits, the label is closed so that all of the labels are QPaintDevice generally not accessible. However, if our program, when the app exits, the component is not closed, which will cause the program to crash .

This time, perhaps know c++11 standard children's shoes think of smart pointers . Yes, the smart pointer is a managed class that can be used as a pointer to implement the automatic release of pointers, but the smart pointer if the use of the same will produce a variety of problems, therefore, the proposal has just started to learn the classmate, can not use the heap on the variable on the first, if you really want to use, Remember to think about content overflow and leak issues and take the necessary precautions or add attributes to variables without using smart pointers.

Label->setattribute (Qt::wa_deleteonclose);

  At this point, we look back at the Exec method, because we built the object on the stack QLabel , but we can always show it there (imagine, if it's not an infinite loop, the main() function will immediately exit, and QLabel the object will of course be directly destructor).

Finally, we enclose the declaration of variables and smart pointers on the heap for reference only.

2.1 On-Heap code reference
1#include <QApplication>2#include <QLabel>3 4 intMainintARGC,Char**argv)5 {6 qapplication A (ARGC,ARGV);7Qlabel *label =NewQlabel ("Hello World");8Label->Show ();9  Ten     returna.exec (); One}
2.2 Smart Pointer Code reference (Sailfish OS)
1#include <QApplication>2 3 intMainintargcChar*argv[])4 {5Qscopedpointer<qapplication> app (Newqapplication (argc, argv));6Qscopedpointer<qquickview> View (NewQquickview);7View->setsource ("/PATH/TO/MAIN.QML");8     ...9     returnApp->exec ();Ten}
third, my point of view

On the use of pointers and the choice of places this is really a matter of opinion, I myself for the use of pointers is very careful, if used also in some non-thread-safe use of the case, and print the log report. Another problem is the release and reuse of pointers, my point of view is that if the pointer is released in the scope of the delete operation, but not NULL, this time the pointer should be able to use, but did not return to the operating system, if there is misunderstanding, please correct me .

My QT5 Learning Path (ii)--The first program (Hello World)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.