[Solved] After removing widgets from layout with vertical spacer the spacer doesn't work anymore
After clearing the content of a vertical layout it seems that the spacer that was there is also removed. At least, after the widgets in the layout are removed, and new widgets are added, the spacer doesn't seem to work.
How can I remove the widgets and keep the verticalSpacer to work?
This is how I remove the widgets from the layout.
while ((child = ui->layout->takeAt(0)) != 0)
QWidget* widget = child->widget();
This is what I want: from startup, with vertical spacer, before clearing
!http://i45.tinypic.com/10o32xh.jpg(With vertical spacer)!
This is what I've got after clearing and adding two widgets, vertical spacer removed or not working?
!http://i50.tinypic.com/35a0u36.jpg(Without vertical spacer)!
It is deleted as it is a child for the layout.
What you should add (I think) is a spacerItem for each clearing so that the widget only displays. And then when you add a new widget just insert a widget in between the spacer items and any previous widgets.
The strange thing is that it actually deletes 2 widges. Debugging shows no layout that's deleted, but still it's gone??
I solved this by inserting a widget and a spacer in the layout. Then I insert/delete items to that widget, not to the layout. The widget grows/shrinks as items are added and deleted and the spacer makes sure that all items are pushed to the top.
Your code removes all the QLayoutItems, including the QSpacerItem, from the QLayoutregardless of whether they contain a widget or not. QLayout::takeAt() removes an item from the layout. Whether you subsequently delete it or its contents is irrelevant: it is no longer part of the layout.
@ChrisW67: Never to old to learn something :p