Berschi Berschi - 1 year ago 63
C++ Question

destructors in Qt4

I'm very confused about using destructors in Qt4 and hope, you guys can help me.

When I have a method like this (with "Des" is a class):

void Widget::create() {
Des *test = new Des;

how can I make sure that this widget is going to be deleted after it was closed?

And in class "Des" i have this:

QPushButton *push = new QPushButton("neu");
QHBoxLayout *layout = new QHBoxLayout;

where and how do I have to delete *push and *layout? what should be in the destructor Des::~Des() ?

Answer Source

Another option to using deleteLater(), or parents, is to use the delete-on-close functionality for widgets. In this case, Qt will delete the widget when it is done being displayed.

Des *test = new Des;
test->setAttribute( Qt::WA_DeleteOnClose );

I like to use it with the object tree that Qt keeps, so that I set delete-on-close for the window, and all widgets in the window have a proper parent specified, so they all get deleted as well.