PySide: Removing a widget from a layout PySide: Removing a widget from a layout python python

PySide: Removing a widget from a layout


Super simple fix:

def deleteButton():    b = layout.takeAt(2)    buttons.pop(2)    b.widget().deleteLater()

You first have to make sure you are addressing the actual button and not the QWidgetItem that is returned from the layout, and then call deleteLater() which will tell Qt to destroy the widget after this slot ends and control returns to the event loop.

Another example illustrates why the problem is occurring. Even though you take the layout item, the underlying widget is still parented to the original layouts widget.

def deleteButton():    b = layout.takeAt(2)    buttons.pop(2)    w = b.widget()    w.setParent(None)

This is not the preferred way, as it still leaves the cleanup of the object ambiguous. But it shows that clearing the parent allows it to leave the visual display. Use deleteLater() though. It properly cleans everything up.