1. An overview of the

  • The last article explained a simple Qt program, mainly to help just contact Qt program beginners, this series of tutorials mainly in pure code way to start, the follow-up can be described according to the progress. UI interface design, this way is relatively easy to learn, use. UI interface can quickly design a simple interface, But beginners should still focus on code to lay a solid foundation for the future.

2. The target

  • The purpose is to understand some classes, such as QPushButton, QMenu class simple application. This article or a new empty project, and then add to it main. CPP file, write. Pro file code as follows:
1. QT       += core gui
2. 
3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4. 
5. 
6. TARGET = demo_02
7. TEMPLATE = app
8. 
9. DEFINES += QT_DEPRECATED_WARNINGS
10. 
11. 
12. SOURCES += \
13.     main.cpp
Copy the code
  • The above code is not much to explain, the last article has been very clear, here is no longer redundant, there is a need to understand the friends can go to the last article, here focuses on the main. CPP file code, main. CPP file is as follows:
1. #include <QApplication>
2. //#include <QtWidgets>
3. #include <QPushButton> // Add the QPushButton header file
4. #include <QMenu> // Add the QMenu class header file
5. #include <QWidget> // Add the QWidget class header file
6. 
7. 
8. int main(int argc, char *argv[])
9. {
10.     QApplication a(argc, argv);
11.     QWidget *widget = new QWidget(); // Create a QWidget object pointer widget. The default parent parameter is 0, so it is a window
12.     widget->resize(450.300); // Set the window size
13. 
14. 
15.     QPushButton *btn1 = new QPushButton(widget); // Create a QPushButton object pointer btn1 and set its parent to the object Widget window, that is, bTN1 is a control on the widget window
16.     btn1->setText(QObject::tr("&Quit")); // Set the content of btn1, specify Alt+Q as acceleration key, here only English letters are supported, use the first letter +Alt as shortcut key
17. 
18. 
19.     QPushButton *btn2 = new QPushButton(widget); // Create a QPushButton object pointer btn2 and set its parent to the object Widget window, that is, the bTN2 is a control on the widget window
20.     btn2->setText(QObject::tr("About Qt (& A)")); // Set the content of btn2, specify Alt+A as acceleration key, here also provides A Chinese display and set shortcut key method
21.     btn2->move(80.0); // Move the button to prevent occlusion
22. 
23. 
24.     QPushButton *btn3 = new QPushButton(widget); // Create a QPushButton object pointer btn3 and set its parent to the object Widget window, that is, the bTN3 is a control on the widget window
25.     btn3->setText(QObject::tr("zoom")); // Set the contents of btn3
26.     btn3->move(160.0); // Move the button to prevent occlusion
27. 
28.     QMenu *menu = new QMenu(); // Create a pointer menu to the QMenu object
29.     menu->addAction(QIcon(".. /demo_02/images/zoom-in.png"),QObject::tr("Enlarge")); // Set the icon, use the relative path, set the dropdown menu for Btn3, this menu does nothing
30.     btn3->setMenu(menu); // Set btn3's parent to menu, that is, set a dropdown menu for Btn3
31. 
32. 
33.     QPushButton *btn4 = new QPushButton(widget); // Create a QPushButton object pointer btn4 and set its parent to the object Widget window, that is, bTN4 is a control on the widget window
34.     btn4->setText(QObject::tr("Help & (H)")); // Set the contents of btn4
35.     btn4->setIcon(QIcon(".. /demo_02/images/help.png")); // Set the icon to use the relative path
36.     btn4->move(240.0); // Move the button to prevent occlusion
37. 
38. 
39.     QObject::connect(btn1,&QPushButton::clicked,&QApplication::quit); // Connect btn1 and close window signal with slot function
40.     QObject::connect(btn2,&QPushButton::clicked,&QApplication::aboutQt);  // Connect btn2 to the signal and slot functions introduced about Qt
41. 
42.     widget->show(); // Make the window object widget created by QWidget display
43. 
44.     int ret = a.exec();
45. 
46.     delete widget; // Destroying the parent widget automatically destroys its children
47. 
48. // delete btn1; // Destroy btn1 and create an object pointer on the heap
49. // delete btn2; // Destroy btn2 and create an object pointer on the heap
50. // delete btn3; // Destroy btn3 and create an object pointer on the heap
51. // delete btn4; // Destroy btn4 and create an object pointer on the heap
52. 
53. 
54.     Return ret; //// the program enters a message loop and waits for a response to user input. Here main() passes control to Qt, which does the event processing and returns the value of exec() when the application exits. In exec(), Qt accepts and processes user and system events and passes them to the appropriate widgets.
55. }
Copy the code
  • The main functions of the program are: Click the Quit button to close the window, click on the button on the Qt, (A) to open the window on Qt is introduced, click on the button to zoom, you can open the drop-down menu, A drop-down menu for amplifying menu items, help button is not set functions, some of these have shortcut setting button and icon set, the above is the main function of this small application, The specific operation effect picture is as follows:

3. Code analysis

  • CPP file used in several classes and functions, by querying the help document to find the use of the function as follows, Qt software learning must make full use of the help document, this is very important, will see the help document will progress quickly. I have annotated the code block in detail, basically every piece of code has a detailed Chinese annotation, you need to look at the code carefully, I will explain a few of the more important places, and share with you my understanding, there are wrong places also hope to correct, we communicate and learn together, common progress. Here are a few function prototypes found in the help documentation:
  1. QWidget::QWidget(QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) constructs a decorative widget without parent and flag.
  2. QPushButton::QPushButton(QWidget *parent = Q_NULLPTR) constructs a button with no text or parent.
  3. QMenu::QMenu(QWidget *parent = Q_NULLPTR) constructs a menu with parent.
  4. QAction *QMenu::addAction(const QIcon &icon, const QString &text) creates a new action with an icon and some text. This function adds the newly created action to the menu’s action list and returns it.

A few notes:

A. The principle of including header files is to include as few header files as possible. The four header files in this article can be replaced by the commented out header file. Only one header file can contain the entire module header file, but it is not recommended to do so.

B. Code: Delete Widget; The destruction of the QWidget window automatically destroys its children, namely the four buttons. Why the QWidget should be destroyed and why the four buttons should also be destroyed is a matter of Qt’s object model (object tree).

4. Object Model (Object Tree)

  • A pointer to a Parent object is provided when an object is created in Qt. Qobjects are organized as a tree of objects. When you create a QObject, you see that the constructor of the QObject takes a pointer to the QObject as an argument, which is the parent object pointer. That is, when we create a QObject, we can provide a parent, and the QObject we create is automatically added to the children() list of its parent. When the parent object is destructed, all objects in the list are also destructed. (Note that the parent object is not a parent in the inherited sense!)

  • QWidget is the parent class of everything that can be displayed on the screen. QWidget inherits from QObject and therefore also inherits this object tree relationship. A child automatically becomes a child of the parent component. Therefore, it is displayed in the parent component’s coordinate system, trimmed by the parent component’s boundaries. For example, if the application deletes a dialog box when the user closes it, then the buttons, ICONS, and so on that belong to the dialog should be deleted altogether. This is the case, because these are all sub-components of the dialog.

  • Of course, we can also delete child objects ourselves, and they are automatically removed from the list of parent objects. For example, when we delete a toolbar, the main window will automatically remove the toolbar from the list of its children and adjust the screen display automatically.

  • Qt introduces the concept of object tree, to a certain extent to solve the memory problem. When a QObject is created on the heap, Qt also creates an object tree for it. However, the order of objects in the object tree is undefined. This means that the order in which these objects are destroyed is also undefined. When a QObject in any object tree is deleted, it is automatically removed from the children() list of the parent if the object has a parent. If there are children, delete each child automatically. Qt guarantees that no QObject will be deleted twice, depending on the destructor order.

  • The Pointers to objects we created with new in this article are all created on the heap, and usually require the programmer to manually free memory. We can discuss the stack in a later article, but these are all C++ basics. Finally, welcome everyone to leave a message exchange, there is a need to code can follow the wechat public number (introduced in the introduction) to send to you, convenient for everyone to debug the program and transplant code.