To get a widget from a QLayout, you have to call its itemAt(index)
method.
As the name of this method implies, it will return an item instead of a widget. Calling widget()
on the result will finally give you the widget:
myWidget = self.myLayout.itemAt(index).widget()
To remove a widget, set the parent widget to None
:
myWidget.setParent(None)
Also really helpfull is the QLayout count()
method. To find and delete all contents of a layout:
index = myLayout.count()
while(index >= 0):
myWidget = myLayout.itemAt(index).widget()
myWidget.setParent(None)
index -=1
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…