title: replacing layout on a QWidget with another layout
body: <p>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 <a href="https://stackoverflow.com/a/9383780/761090">this answer</a>:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>How can I get rid of the old layout and set the new one?</p>
  
  <hr>
  
  <p>The documentation is <a href="http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwidget.html#setLayout" rel="noreferrer">quite terse</a> and apparently not directly applicable to python:</p>
  
  <blockquote>
    <p>QWidget.setLayout (self, QLayout)</p>
    
    <p>The QLayout argument has it's ownership transferred to Qt.</p>
    
    <p>Sets the layout manager for this widget to layout.</p>
    
    <p>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.</p>
    
    <p>If layout is the layout manger on a different widget, setLayout() will
    reparent the layout and make it the layout manager for this widget.</p>
    
    <p>Example:</p>
  
  <pre><code> QVBoxLayout *layout = new QVBoxLayout;
   layout-&gt;addWidget(formWidget);
   setLayout(layout);
  </code></pre>
    
    <p>An alternative to calling this function is to pass this widget to the
    layout's constructor.</p>
    
    <p>The QWidget will take ownership of layout.</p>
    
    <p>See also layout() and Layout Management.</p>
  </blockquote>