[Interest] Taking back a widget from a QBoxLayout?

Matthew Woehlke mwoehlke.floss at gmail.com
Fri Feb 22 23:35:37 CET 2019

On 22/02/2019 15.19, Jason H wrote:
> 1. 
> '''
> void QLayout::addItem(QLayoutItem *item)
> ...
> void QLayout::addWidget(QWidget *w)
> Adds widget w to this layout in a manner specific to the layout.
> This function uses addItem().
> '''
> How can addWidget(QWidget *w) use addItem(QLayoutItem *item) ? 
> I assume by use, it means "call"? Layouts and Widgets only share QObject...

Internally, that function creates a QLayoutItem for the widget (at
least, I assume so WCTS┬╣).

(┬╣ https://cygwin.com/acronyms/)

> 2. Furthermore, I am confused about "ownership" vs  "parent"ing

"Ownership" is used in the general C++ sense, i.e. who is responsible
for deleting an object (C++ sense, not QObject) when it is no longer
needed. Parenting refers to a special property of some Qt classes by
which they "own" certain objects.

I would say that, generally speaking "parenting" is "ownership" plus
additional stuff, e.g. parenting also has implications for how objects
will interact, and often the child knows the identity of its parent.

> and why the note about the reparenting during add is not mentioned
> in the docs.

Hmm... *that* is a fair question :-).

> "Note: The ownership of item is transferred to the layout, and it's 
> the layout's responsibility to delete it."
> However, deletion happens when the parent is deleted. This is 
> confusing several concepts. Why would the layout ever be responsible
> for deleting it?

The layout isn't responsible for deleting the widget. It *is*
responsible for deleting the layout item. This is partly a convenience,
but may be a *necessity*... considering that some functions internally
generate layout items, it would be awkward if the caller was responsible
for later deleting those.


More information about the Interest mailing list