Does QLayoutItem delete its managed item in its destructor?
-
I think the title states the question clearly.
Background:
Qt version 5.15
The documentation of QLayoutItem doesn't say anything, but the documentation of QLayout::takeAt has an example code fragment that suggests that the managed item will not be destroyed.
However checking my own program the debugger stack trace seems to indicate that it will be destroyed.I know that the answer is in the code, but sometimes Qt code can be very hard to read, so I'm hoping that someone knows the answer.
-
QLayoutItem
does not delete anything, same as allQLayouts
.
QLayoutItems
are not evenQObjects
, so they cant have anyQWidget
child widgets, which they could delete in their d'tors.
The parent widget, on which the layout is installed, can/will delete the child widgets.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.
-
@Pl45m4 said
QLayoutItem
does not delete anything, same as allQLayouts
.QLayoutItem
is not a layout, rather, it is a layout item, so I don't understand your comment.
On the contrary, layouts are also layout items (it is so in order to allow sublayouts).QLayoutItems
are not evenQObjects
, so they cant have anyQWidget
child widgets, which they could delete in their d'tors.
The parent widget, on which the layout is installed, can/will delete the child widgets.True, they are not
QObject
s, but they still have a destructor.
And they still have a managed item (unless empty), which can be a widget, layout, or spacer, according to the documentation ofQLayoutItem
.Note: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself.
This looks useful information.
However, I never talked about widgets.
In fact the layout item I was using was itself a layout, I didn't mention because I didn't think it would make any difference, now I understand I should have.
If i understand that correctly, layouts delete their child layouts in their destructor.
Is it done through the layout item destructor? -
@_ said in Does QLayoutItem delete its managed item in its destructor?:
@Pl45m4 said
QLayoutItem does not delete anything, same as all QLayouts.
QLayoutItem is not a layout, rather, it is a layout item, so I don't understand your comment.
On the contrary, layouts are also layout items (it is so in order to allow sublayouts).It means what it says.
@_ said in Does QLayoutItem delete its managed item in its destructor?:
If i understand that correctly, layouts delete their child layouts in their destructor.
Is it done through the layout item destructor?Check it yourself.
QLayoutItem
destructor does basically nothing, except destroying theQLayoutItem
itselfwhereas, if you delete a
QLayout
, it will delete all of its child-layouts, but not their widgets