PDA

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



Momergil
13th October 2013, 04:46
Hello!

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?


Thanks,

Momergil

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

anda_skoa
13th October 2013, 09: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);
dialog->show();


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.

Cheers,
_