Vadim Shestak
Vadim Shestak

Reputation: 49

How does operator delete work in qt5 with qObjects

In c++ when u allocate memory, u should always delete them (in Destructors for example). But in qt u often don't worry about deleting objects. Qt does it for u. IDE does not correctly show all the memory leaks. I saw code somewhere like this:

anyLayout->addWidget(new QLabel(QString("text")));

will this QLabel be truly memory leak?

The same question about adding the same way QListString to QComboBox.

Upvotes: 2

Views: 231

Answers (1)

László Papp
László Papp

Reputation: 53215

No, QWidgets added to a layout will be automatically parented to the layout. This is explained here.

Note: The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.

When the parents are cleaned up, so will the children. I would encourage you to read about Object Trees & Ownership in Qt.

Upvotes: 4

Related Questions