[Solved] Confused about ownership of widgets in a layout
-
Today I got confused about who is supposed to own widgets that are managed by a layout.
According to the Layout Documentation (http://doc.qt.io/qt-5/layout.html):
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, looking at the different examples of
takeAt()
that are available for instance here (http://doc.qt.io/qt-5/qlayout.html#takeAt) and there (http://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-example.html) and additionally the destructor of the FlowLayout class, the layout classes are responsible for deleting widgets, which seems to contradict exactly the note above.So, which one is correct? Is the note above a mistake?
-
Afaik it deletes only a QLayoutItem. The Widgets created in the window.cpp of your example doesn't contain a parent. That means that it destroys the QLayoutItem, but the widgets are only getting destroyed on application exit. I might be wrong but that's what i see on first look.
You can check it by keeping a reference to the widgets and destroy the FlowLayout by hand. If the widgets aren't destroyed, then your FlowLayout just destroys the QLayoutItem but keeps the widgets alive.