Basic knowledge
Confidential MainWindow :: MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui :: MainWindow)
After just creating a qt project, I was caught by a file in the middle. Because I didn't understand this syntax (what does this thing appear after inheriting the QMain class...what is initialized by the following initialization list ui (new Ui::MainWindow)), I expanded it with the thought of getting to the bottom of it analyze. Picture below:
First, let's disassemble the syntax of ui (new Ui::MainWindow):
ui is an object pointer that has been declared in advance in the mainwindow header file. Because this type is a type with the same name from a different domain, domain resolution is added in front
So obviously back to the previous program, new Ui::MainWindow is initializing the ui pointer
So where is the type body in the namespace Ui declared here? Let's look for a statement
The principle has a hidden header file. . . . Then things become clear, let me explain briefly:
At the entry of the program, the type under the field of Ui is declared, and the pointer of this type is declared
1
QT_BEGIN_NAMESPACE
1
namespace Ui { class MainWindow; }
1
QT_END_NAMESPACE
↓ ↓
Then the Ui_MainWindow type is implemented in the hidden header file and Ui::MainWindow is derived
↓ ↓
Finally, after the inheritance in the cpp file corresponding to the program entry header file, use the initialization list to initialize the pointer
After the interpretation, I sighed that C++ is broad and profound.
Original link: https://www.cnblogs.com/xiandong-chen/p/16057507.html