title: PySide: Removing a widget from a layout
body: <p>I'm trying to remove a Qt widget from a layout in a PySide application.</p>
  
  <p>Here is a minimal example. It is a widget with 5 buttons in it, and the middle one is supposed to remove itself when clicked:</p>
  
  <pre><code>import sys
  from PySide import QtGui
  
  app = QtGui.QApplication(sys.argv)
  widget = QtGui.QWidget()
  layout = QtGui.QVBoxLayout()
  buttons = [QtGui.QPushButton(str(x)) for x in xrange(5)]
  
  def deleteButton():
      b = layout.takeAt(2)
      buttons.pop(2)
      del b
  buttons[2].clicked.connect(deleteButton)
  
  map(layout.addWidget, buttons)
  widget.setLayout(layout)
  widget.show()
  app.exec_()
  </code></pre>
  
  <p>What actually happens is this:</p>
  
  <p><img src="https://i.stack.imgur.com/hvzEj.png" alt="What actually happens"></p>
  
  <p>The button is unclickable and clearly isn't taken into consideration for the layout computations, but its image stays in place.</p>
  
  <p>According to the <a href="http://qt-project.org/doc/qt-4.8/qlayout.html#takeAt">Qt documentation</a>, the correct way of deleting all objects from a layout is:</p>
  
  <pre><code>while ((child = layout-&gt;takeAt(0)) != 0) {
      delete child;
  }
  </code></pre>
  
  <p>Here I just want to delete the third button, so I just call <code>takeAt(2)</code>, and then <code>del b</code> to call the destructor on that item. The button object is also <code>.pop</code>'d from the <code>buttons</code> list to make sure there is no leftover reference to the object. How does my code differ from the one in the Qt docs that would cause such a behavior?</p>