[Interest] Taking back a widget from a QBoxLayout?

Jason H jhihn at gmx.com
Fri Feb 22 20:42:49 CET 2019



> Sent: Friday, February 22, 2019 at 2:27 PM
> From: "Giuseppe D'Angelo via Interest" <interest at qt-project.org>
> To: interest at qt-project.org
> Subject: Re: [Interest] Taking back a widget from a QBoxLayout?
>
> Il 22/02/19 19:04, René J.V. Bertin ha scritto:
> >> I am not 100% sure, it's been a while, but I would assume that the layout is not the true parent, combined is.
> > The docs aren't exactly clear on this subject, at least not with the sort of reading glasses I usually have on when I don't exactly want to be reading them ;)
> 
> Widgets can only have other widgets as parents.
> 
> When you add a widget to a layout, the layout will reparent that widget 
> to the widget the layout is installed onto.
> 
> > https://doc.qt.io/qt-5/layout.html#tips-for-using-layouts
>From that: 
'''
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.
'''

However:
'''
void QLayout::addItem(QLayoutItem *item)
Implemented in subclasses to add an item. How it is added is specific to each subclass.

This function is not usually called in application code. To add a widget to a layout, use the addWidget() function; to add a child layout, use the addLayout() function provided by the relevant QLayout subclass.

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

void QLayout::addWidget(QWidget *w)
Adds widget w to this layout in a manner specific to the layout. This function uses addItem().
'''

So the docs are clear as mud. QLayout mentions "ownership" but the tips mention "parent". FWIW, I knew QWidgets parented QWidgets, but I don't know what it means by "ownership". Typically, "ownership" is talked about w.r.t. Python or QML, where the garbage collection schemes differ. It would be nice to amend the docs to clarify this.



More information about the Interest mailing list