Chapter 1 getting started
each class in QT has a header file of the same name, which contains its class definition. For example, to use the qapplication class, you must add "# include " in Program .
the qapplication class is used to manage resources within the application scope. Its constructor requires argc and argv as parameters.
the widget indicates "window gadget", which is the equivalent of controls and containers in Windows terminology.
widgets are invisible when they are created (always created hidden ).
widgets can accommodate other widgets.
event, event loop
Widgets in QT are emit signal when user behavior or status changes. Signal can be connected with the slot function (CONNECT), so that when signal is emit, the corresponding slot function will be automatically called.
The constructor of the qwidget class requires a qwidget * pointer as the parameter, indicating its parent widget (the default value is 0, that is, there is no parent widget ). When a parent widget is deleted, QT automatically deletes all its child widgets.
Layout manager: an object used to manage the size and position of the widget.
There are three layout manager classes in QT: qhboxlayout, qvboxlayout, and qgridlayout.
The basic mode is to add widgets to layout. layout automatically takes over the widget size and position.
You can use the-style parameter to change the default explicit style when running the QT program.
Chapter 2 creating dialogs
Section 1 subclassing Dialog
In QT, the base class of all dialog is qdialog. Qdialog is derived from qwidget.
Forward Declaration
In QT, all classes that define signal or slot must use the q_object macro at the beginning of the class definition.
The signal keyword in QT is actually a macro definition. Similarly, the slots keyword is also a macro definition.
<Qtgui>. The header file contains the qt gui classes.
Qt provides the following classes: qtgui, qtnetwork, qtopengl, qtsql, qtsvg, and qtxml.
The qobject: TR () function converts the input string to another language (international ). It is a good habit to use the tr () function for strings visible to all users.
"&" In the string indicates the shortcut key.
(Concept) Buddy: Two widgets A and B. If a has a shortcut key, when the user presses the shortcut key, the input focus of the program is automatically transferred to B, and B is called a buddy.
Qwidget: Close () is a slot. Its default behavior is to hide the corresponding widget but not delete it.
Layout can contain widgets and other layout items. By nested use of qhboxlayout, qvboxlayout, and qgridlayout, a very complex dialog can be constructed.
It is worth noting that the layout manager class does not belong to widgets. In fact, it is derived from qlayout, and qlayout is derived from qobject.
Qwidget: sizehint () returns the ideal size (ideal size) of a widget ).
The emit keyword is unique to QT and is used to release signal.
MoC stands for Qt's meta-object-compiler.
All classes that use the q_object macro must be processed by MOC during compilation. Otherwise, a link error occurs. The solution to this error is also simple. Re-Execute qmake to update the makefile and re-compile it.
section 2 signal and slot in depth
the signal & slot mechanism is the foundation of QT.
the slot is almost identical to the common C ++ class member functions. They can be virtual and can be overloaded. They can be public, protected, or private. They can also be directly called by other member functions.
the connection between signal and slot is one-to-one, one-to-many, or multiple-to-one.
signal and signal can also be connected. The difference between this situation and signal-slot is that when the first Sigal is emit, the second signal is also emit.
you can call disconnect () to cancel the relationship between signal and slot. Generally, you rarely need to explicitly call disconnect () because when an object is deleted, qt automatically removes related contacts.
the relationship between sigal and slot or signal requires that the two have a consistent parameter list. In addition, IF signal has more parameters than the slot, extra parameters will be ignored.
do not create a mindset: the signal-slot mechanism can only be used for widgets. In fact, the signal-slot mechanism is implemented by qobject and is not limited to Gui programming, but can be used in any qobject subclass.
Section 3 Rapid Dialog design
The form created using QT designer is eventually converted to C ++Code.
Qmake
The tool detects interface file (*. UI files) and calls UIC, that is, the user interface of QT.
Compiler. UIC converts the. UI file to C ++ code and saves it in a file in the form of ui_xxx.h. This file defines the corresponding dialog class and contains
Setupui () member function, used to initialize form.
Note that the class created by UIC is not derived from any QT class.
Qt
The parent-child mechanism of is implemented by qobject. If parent is specified when an object is created, parent adds the object to its childern
List. When a parent is deleted, QT traverses its childern list and deletes each child. This process is recursive.
Qt's parent-child mechanism greatly simplifies memory management and reduces the risk of Memory leakage. In this mechanism, we only need to explicitly Delete objects created through new without parent.
For widgets, parent has an additional meaning: chidl widgets are explicitly within the range of parent widgets. If you delete a parent widget, not only the child widget is released from the memory, but also disappears on the screen.
Qdialog: accept () sets the return value of dialog to qdialog: accepted (equal to 1), while qdialog: reject () sets the return value to qdialog :: rejected (equal to 0 ).
Section 4. shape-changing Dialog
Section 5. Dynamic dialogs
Dynamic dialog refers to the dialog created based on the. UI file when the program is running. This type of dialog does not convert. UI to C ++ code through UIC, but instead uses the quiloader class to load. UI files at runtime.
You can use qobject: findchild <t> () to access the child widget of form.
To use quiloader, add the following content to the. Pro file of the QT program: config + = uitools
Dynamic dialog allows you to change the form layout without re-compiling the program.
Section 6. Built-in widget and dialog classed