The parent parameter in Qobject in QT

Source: Internet
Author: User

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

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.