Introduction to the Qt::wa_deleteonclose of Qt window properties

Source: Internet
Author: User

First, brief

Today, we introduce a simple window property--qt::wa_deleteonclose.

After the normal window is created, we usually call the close () method to close the window, and here we look at the introduction to the Close () method in the Q helper.

BOOL Qwidget::close ()
Closes this widget. Returns true if the widget was closed; otherwise returns false.

First it sends the widget a qcloseevent. The widget is hidden if it accepts the close event. If it ignores the event, nothing happens. The default implementation of Qwidget::closeevent () accepts the close event.

The If the widget has the Qt::wa_deleteonclose flag and the widget is also deleted. A Close events are delivered to the widget no matter if the widgets are visible or not.

When the close () method is called, it first sends a shutdown event (qcloseevent) to the widget. If the widget accepts a close event (qcloseevent), the window will be hidden (actually called hide ()). If the widget does not accept the Close event, then the window will do nothing. By default the widget accepts the shutdown event, and we can override the Qcloseevent event to accept or not accept.

If the widget sets the Qt::wa_deleteonclose property, the widget will be freed. The close event is passed to the widget, regardless of whether the widget is visible. That is, after receiving the Qcloseevent event, in addition to calling the Hide () method to hide the window, the Deletelater () method is called to free the window and no longer consumes resources.

Therefore, calling close () does not necessarily destroy the Window object. Only the Qt::wa_deleteonclose property is set to delete the Destroy. If this property is not set, the function of close () and hide (), setvisible (false), hides only the Window object, and does not destroy the object.

Explanation of the Qt::wa_deleteonclose attribute in Qt Helper

Qt::wa_deleteonclose makes Qt Delete this widget, the widget has a accepted the Close event (see Qwidget::closeevent ()).

If the window is set to qt::wa_deleteonclose this property, QT releases the resources that the window occupies when the window accepts the Shutdown event.

this->setAttribute(Qt::WA_DeleteOnClose);
    • 1
    • 1

So if we create a window in the program through new , you can set the Qt::wa_deleteonclose property for that window. This way, when the window is closed, QT can automatically reclaim the resources occupied by the window, so that the invalid resources can be reclaimed in time, which is useful for saving memory space.

Here's a little test I made by constantly opening the window and closing all open windows. In Task Manager, compare the size of the resources that the application occupies. Set the Qt::wa_deleteonclose Property

Not set Qt::wa_deleteonclose Property

As you can see from the last two pictures, the program takes up more memory while the windows are constantly open, and when all the windows are closed, we find that the memory resources occupied by the closed windows are constantly being recycled in the case of the Qt::wa_deleteonclose property . Revert to the previous state after closing all. Without setting the Qt::wa_deleteonclose property , we find that the closed window consumes no less memory resources. This means that the closed window memory has not been reclaimed.

Thus it seems that the importance of the Qt::wa_deleteonclose attribute .

http://blog.csdn.net/goforwardtostep/article/details/53647146

Introduction to the Qt::wa_deleteonclose of Qt window properties

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.