[Interest] Taking back a widget from a QBoxLayout?

Matthew Woehlke mwoehlke.floss at gmail.com
Fri Feb 22 21:10:04 CET 2019

On 22/02/2019 14.42, Jason H wrote:
>>> 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)
> [...]
> 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().
> '''

...but that's talking about a *QLayoutItem*, which is *not* a QWidget
(or even a QObject). It's a class that encapsulates a "thing" (widget,
other layour, spacer, ...) that is present in a QLayout.

Let's say you have widgets P and C, with P having layout L which
contains C.

P "owns" C and (AFAIK) L.

There *also* exists a layout item I. L owns I. I *references*, but does
not own, C.


More information about the Interest mailing list