[SOLVED]Little question about memory when delete
I'm currently using a window which has a layout principal. It's contains loads of object such as QPushButtons, QLabel...
In my destructor, I destroy the layout and all the widgets.
Ad the layout countains all the widget, I was wondering if doing just "delete layout" would be enough to get a clean memory.
the layout and widgets are all childs of the window. When the parent is destroyed so will the children.
And the widget included in a layout are also considered as children of the layout?
you only have to destroy the window. The layout and all it's children will be destroyed automatically.
I you're using code not Qt designer, make sure you use the parent parameter where needed.
The layout has no widgets as children. The layout is some organizational stuff, that is attached as child to a widget. The widgets inside a layout are chidlren of the parent of the layout.
Deleting the main window containing the top layout is enough to delete all children (widgets, layouts and other QObject derived classes)
Look at "Tips for Using Layouts" section.
Tips for Using Layouts
When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.
Note: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself. Widgets can only have other widgets as parent, not layouts.
You can nest layouts using addLayout() on a layout; the inner layout then becomes a child of the layout it is inserted into.
Hi Vijay Bhaska Reddy, The question was about memory, and when it is deleted, it was not related to layouts in general.
It's not so simple tho, I really have to check each widget if they are only parented to the layout and not another widget, if I followed what you all said.
Thanks for all the answers.
They are reparented by default. If you add hem to the layout, the layout will reparent them to it#s widget. It works.
It's actually really simple. If you've added a widget X to a layout or widget that is part of another widget Y, X will be deleted when Y is deleted. This works recursively, so you really just have to delete the top-level widget.