I’m wondering for a class like the following , is that necessary to call delete mainLayout manually ?
class Dummy : public QWidget {
public:
Dummy() { mainLayout = new QHBoxLayout(); setLayout(mainLayout); }
~Dummy() { delete mainLayout; }
private:
QHBoxLayout *mainLayout;
}
Would QApplication release all its child widgets automatically ?
When the Dummy object destructor is called, its base class QWidget destructor will also be called (by C++), and the QWidget destructor takes care of deleting any widgets whose parent is this Dummy object. That is, each child of this Dummy object is automatically deleted.
This then recurses, so the children of all the children are deleted.
So, when shutting down a Qt application, the only QWidgets (well, actually QObjects) you need to manually delete are those whose parent is 0, i.e. the top-level ones. Their destructors will then automatically ensure all their children are destroyed.
As documented in the Qt namespace page, there is also the flag Qt::WA_DeleteOnClose. This:
I think that this flag is not commonly used, though. So a good general rule is just to make sure you delete your top-level widgets when the application shuts down.