View Full Version : Difference between WA_DeleteOnClose = true and deleteLater()

13th October 2013, 05:46

My question: is there a difference between using setAttribute(Qt::WA_DeleteOnClose, true); and deleteLater(); ?

To give a context: I want to create a modeless dialog, so I may use it and its parent at the same time. But I also don want just to create a pointer to it using show() and no delete ... to avoid unecessary memory comsumption when the dialog is closed and its parent isn't yet closed. So I figured out that, if I put deleteLater() in the closeEvent(QClosEvent*); reimplemented function or if I use the window attribute, setting it in the constructor, either way the object is destroyed when the dialog is closed.

A second question is, which of them should I use preferentially?



13th October 2013, 09:46
Qt::WA_DeleteOnClose setting makes a call to the widget deleteLater. If you do not have to override the methods that do not.

13th October 2013, 10:47
My personal preference would be to use the widget attribute and to set it from the code that creates the dialog, i.e.

MyDialog *dialog = new MyDialog(this);
dialog->setAttribute(Qt::WA_DeleteOnClose, true);

The main motivation for doing it in such an explicit way is that anyone reading the code of the instatiation immediately sees that the deletion is taken care of.