Today, a small program was written to verify the role of the parameter parent in the constructor with parameters.
Declare a pointer of type Qdialog in MainWindow and initialize it in MainWindow. I used two types of initialization, one is a constructor with parameters, and one is a constructor without parameters. As follows:
/* mainwindow.h */ *dialog, ... .. /* Mainwindow.cpp */ // The first method, initialized with a construct without a parameter New Qdialog; // The second way, initialized with the constructor function with a parameter New Qdialog (this);
Dialog->show ();
The display of the two methods is different, the first way in the taskbar will have the location of the dialog box, that is, although the member variables in the MainWindow, but in the display and MainWindow-lateral. Closing the Mainwindow,dialog will not be turned off. The second way is only MainWindow in the taskbar and does not appear in the dialog position. And the Close Mainwindow,dialog dialog box is automatically closed.
This is caused by the parent parameter, which in QT specifies the parent component of the component, such as when the dialog initialization does not specify its parent component, then it is a top-level container, so the taskbar will leave it a position. The advantage of using the parent parameter to indicate the component's parental component is that when the parent component is deleted, QT can guarantee that all the components that refer to the parent pointer to the current component will be deleted. QT can prevent memory leaks by traversing the parent property.
The parent parameter in Qobject in QT