I have a widget which changes when an option is toggled. This invalidates all layouts and widgets. I keep list of all layouts, so I can delete them using something similar to this answer:
class MyWidget(QFrame):
# ...
def reLayout(self):
def deleteLayoutChilds(l):
while l.count():
item=l.takeAt(0)
widget=item.widget()
if widget: widget.deleteLater()
else: deleteLayoutChilds(item.layout())
for l in self.allLayouts: deleteLayoutChilds(l)
# now install the new layout
##
## how to delete the old layout first?
l=self.layout(); del l # no effect
#
layout=QGridLayout(self)
## warning: QLayout: Attempting to add QLayout "" to MyWidget "", which already has a layout.
How can I get rid of the old layout and set the new one?
The documentation is quite terse and apparently not directly applicable to python:
QWidget.setLayout (self, QLayout)
The QLayout argument has it's ownership transferred to Qt.
Sets the layout manager for this widget to layout.
If there already is a layout manager installed on this widget, QWidget won't let you install another. You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout.
If layout is the layout manger on a different widget, setLayout() will reparent the layout and make it the layout manager for this widget.
Example:
QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(formWidget); setLayout(layout);
An alternative to calling this function is to pass this widget to the layout's constructor.
The QWidget will take ownership of layout.
See also layout() and Layout Management.