My QT5 learning path (2) -- the first program, the first qt5 learning path

Source: Internet
Author: User

My QT5 learning path (2) -- the first program, the first qt5 learning path
I. Preface

"To do something better, you must first sharpen the tool." in the previous section, I introduced the installation and configuration methods of Qt and set up a basic development platform. In this section, we will use a simple example to understand the Qt programming style and specifications ~~~

2. The first program-Hello World

First, we can follow the method described in the previous section to create a new project. The project name can be "Hello. In the process of creating a project, there is an option to choose whether to create a view interface. This option can be left blank first, because we only understand the Qt mechanism, you don't need Qt to do much for us. After the creation is complete, Open main. cpp.

My main tasks are:

1. shield the program's own Dialog Box program code;

2. Add a label control and pass a text value to it.

The final result is as follows:

Next, let's analyze the basic process of Qt. 1 ~ The three lines are header file inclusion. There are two types of header files. The first is a custom header file or a local header file, which is expressed and contained with "", and the second is a system header file, here is the header file of Qt, which can be expressed and included directly with <>. Row 7th is used to create a QApplication instance. For Qt programs,main()A function is generally used to create an application Object (the GUI program isQApplicationNon-GUI program isQCoreApplication.QApplicationActuallyQCoreApplication.), This object is used to manage the lifecycle of the Qt program and enable the event loop. 10 ~ Line 11 is the core code, that is, the actually added use case code. Here I create a QLabel, assign values to it using the constructor, and call the show method to display it. The last line calls exec to enable the event loop (which can be understood as a wireless loop ).

After writing these two sentences of code, let's have a question. Here we will not discuss the message mechanism of Qt and other communication principles, simply consider the stability and robustness of the program from the aspect of C ++.

Question 1: do I create a QLabel on the stack or on the stack?

Question 2: If I create a QLabel variable as a heap variable, what should I pay attention?

Let's first discuss Question 1. There should be no controversy. The Qlabel variable is created on the stack. Let's take a look at Question 2. If I declare a variable as a heap variable because of some requirements, I will allocate space for this variable at this time. At this time, the problem arises. Allocate space. Who will release it after the program ends? What should I do if memory leaks? How can we prevent memory leakage? If we do not care about it, the operating system will recycle it after the program ends. However, we can see that the label is built on the stack, and the app is built on the stack. This means that the label will be analyzed after the app. That is, the life cycle of the label is longer than that of the app.This is a taboo in Qt programming.. Because in Qt, allQPaintDeviceMust haveQApplicationCreate and use instances. If you are curious, you can say,QLabelInherited fromQWidget,QWidgetIsQPaintDevice. The reason for the above Code is that the label is disabled when the app exits.QPaintDeviceGenerally, it will not be accessed. However, if our program,When the app exits, the component is not closed, which causes the program to crash..

In addition, another reason why the program does not crash isIf it is at the end of the main function, it can not be released; at the end of other areas, the new memory is released in reverse order, which is the c ++ standard..

At this time, we may have thought of C ++ 11 standard shoes.Smart pointer. Yes, smart pointers can be used as pointer hosting classes to automatically release pointers. However, poor use of smart pointers can also cause various problems. Therefore, it is recommended that you do not need to use these variables if you are new to learning, remember to think about content overflow and leakage and take necessary preventive measures or not use smart pointers to add attributes to variables.

label->setAttribute(Qt::WA_DeleteOnClose);

  Now, let's look back at the exec method. Because of this, we builtQLabelObject, but it can always be displayed there (imagine, if it is not an infinite loop,main()The function exits immediately,QLabelOf course, the object is directly analyzed ).

Finally, we will attach the declaration method of stacking variables and smart pointers for your reference only.

2.1 stack code reference
 1 #include<QApplication> 2 #include<QLabel> 3  4 int main(int argc ,char **argv) 5 { 6     QApplication a(argc,argv); 7     QLabel *label =new QLabel("Hello world"); 8     label->show(); 9  10     return a.exec();11 }
2.2 intelligent pointer code reference (Sailfish OS)
 1 #include <QApplication> 2  3 int main(int argc, char *argv[]) 4 { 5     QScopedPointer<QApplication> app(new QApplication(argc, argv)); 6     QScopedPointer<QQuickView> view(new QQuickView); 7     view->setSource("/path/to/main.qml"); 8     ... 9     return app->exec();10 }
Iii. My point of view

The usage of pointers and the selection of places are indeed wise, and I am very careful with the usage of pointers, if it is used, it will also be used in cases that do not involve thread security and print the log report. Another problem is the release and reuse of pointers. In my opinion, if the delete operation is performed in the scope where the pointer is released, but it is not set to null, in this case, the pointer is still usable, but it is not returned to the operating system,Please correct me if you have any misunderstanding.

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.