title: QT5.4 remove and delete widget in a layout
body: <p>I have a <code>QStackedLayout</code> which contains few <code>QWidget</code> on it. The widgets layout is <code>QVBoxLayout</code> which have also few <code>QPushButton</code>. What I wanted to do is to remove all the widgets inside the <code>QStackedLayout</code> and then delete the widgets and layouts <strong>BUT</strong> I want all the buttons not to be deleted 'cause I will put them to another widget later.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>while (QLayoutItem *item = m_stacked_layout-&gt;takeAt(0)) {
      QWidget *w = item-&gt;widget();
  
      for (int i = 0; i &lt; w-&gt;layout()-&gt;count(); i++) {
          QPushButton *button = qobject_cast&lt;QPushButton *&gt;(w-&gt;layout()-&gt;itemAt(i)-&gt;widget());
          if (button) {
              w-&gt;layout()-&gt;removeWidget(button);
          }
      }
  
      delete w;
      delete item;
  }
  </code></pre>
  
  <p>The application crashes at the line <code>delete w;</code>. And, if I remove the <code>delete</code> lines, application works fine.</p>