[SOLVED]Little question about memory when delete
-
wrote on 30 Jun 2011, 10:10 last edited by
Hello,
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.Thanks
-
wrote on 30 Jun 2011, 10:23 last edited by
the layout and widgets are all childs of the window. When the parent is destroyed so will the children.
-
wrote on 30 Jun 2011, 10:27 last edited by
And the widget included in a layout are also considered as children of the layout?
-
wrote on 30 Jun 2011, 10:35 last edited by
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.
-
wrote on 30 Jun 2011, 10:38 last edited by
Hi,
one clearification:
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)
-
wrote on 30 Jun 2011, 11:18 last edited by
http://doc.qt.nokia.com/latest/layout.html
Look at "Tips for Using Layouts" section.
EDIT:
Tips for Using LayoutsWhen 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.
-
wrote on 30 Jun 2011, 11:55 last edited by
Hi Vijay Bhaska Reddy, The question was about memory, and when it is deleted, it was not related to layouts in general.
-
wrote on 30 Jun 2011, 12:47 last edited by
Okay,
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.
-
wrote on 30 Jun 2011, 12:56 last edited by
They are reparented by default. If you add hem to the layout, the layout will reparent them to it#s widget. It works.
-
wrote on 30 Jun 2011, 12:58 last edited by
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.
7/10