Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call destructor when closing QDialog started from Q

Tags:

c++

dialog

qt

I made a custom dialog that is derived from QDialog. If i close the dialog, the destructor is not called. Here is how i open the dialog test in the class AWidget. test is a member of AWidget:

//In AWidget:
test = new myDialog();
test->show();
...

If i close this dialog, the dialog test is (of course?) is not deleted/destroyed.

I have to do it in the destructor of AWidget:

Destructor of AWdiget:
delete test;

The myDialog consumes much memory. The user behaviour is to open a myDialog, work in it, close it but to leave AWidget open.

How do i delocate the memory/ call the destructor of myDialog test when closing it?

Edit

Is there maybe a better way than:

void myDiaglog::closeEvent(QCloseEvent* event){
delete this;
}
like image 577
DanK0904 Avatar asked Dec 07 '25 09:12

DanK0904


1 Answers

If you want to always delete the dialog after it is closed you may use Qt::WA_DeleteOnClose:

Makes Qt delete this widget when the widget has accepted the close event (see QWidget::closeEvent()).

//In AWidget:
test = new myDialog();
test->setAttribute (Qt::WA_DeleteOnClose);
test->show();
...
like image 169
Innokentiy Alaytsev Avatar answered Dec 08 '25 21:12

Innokentiy Alaytsev