title: Convert QStackedLayout to QHBoxLayout dynamically
body: <p>I would like to change my widget layout from <code>QStackedLayout</code> to <code>QHBoxLayout</code> to <code>QVBoxLayout</code> dynamically by clicking on push buttons. I am able to switch from <code>QVBoxLayout</code> to <code>QHBoxLayout</code> and vice versa, but my approach does not work for <code>QStackedLayout</code>. I've exhausted all options I can think of. A sample code is attached. Does anyone know how I can achieve my objective?</p>
  
  <pre><code>#ifndef WIDGET_H
  #define WIDGET_H
  
  #include &lt;QWidget&gt;
  #include &lt;QPushButton&gt;
  #include &lt;QHBoxLayout&gt;
  #include &lt;QVBoxLayout&gt;
  #include &lt;QStackedLayout&gt;
  #include &lt;QGridLayout&gt;
  #include &lt;QLabel&gt;
  
  class Widget : public QWidget
  {
      Q_OBJECT
  
  public:
      Widget(QWidget *parent = 0);
      ~Widget();
      QPushButton *button1_;
      QPushButton *button2_;
      QPushButton *button3_;
  
      QHBoxLayout *hLayout_;
      QVBoxLayout *vLayout_;
      QStackedLayout *sLayout_;
      QVBoxLayout *gLayout_;
  
  public slots:
      void layoutHorizontal();
      void layoutVertical();
      void layoutStacked();
  
  private:
      bool isStackedLayout_;
      QLabel *bar_;
  };
  
  #endif // WIDGET_H
  
  
  #include "widget.h"
  #include &lt;QtAlgorithms&gt;
  #include &lt;QDebug&gt;
  
  Widget::Widget(QWidget *parent) : QWidget(parent){
  
      bar_ = new QLabel(tr("TEST!"));
  
      button1_ = new QPushButton(tr("to Horizontal Layout"),(bar_));
      button2_ = new QPushButton(tr("to Vertical Layout"),(bar_));
      button3_ = new QPushButton(tr("to Stacked Layout"),(bar_));
  
      button1_-&gt;setStyleSheet("background: rgba(255,255,0,255);");
      button2_-&gt;setStyleSheet("background: rgba(255,0,255,255);");
      button3_-&gt;setStyleSheet("background: rgba(0,255,255,255);");
  
      connect(button1_,SIGNAL(clicked()),this,SLOT(layoutHorizontal()));
      connect(button2_,SIGNAL(clicked()),this,SLOT(layoutVertical()));
      connect(button3_,SIGNAL(clicked()),this,SLOT(layoutStacked()));
  
  
      gLayout_ = new QVBoxLayout;
      setLayout(gLayout_);
  
      hLayout_ = new QHBoxLayout(bar_);
      hLayout_-&gt;setObjectName(tr("currentLayout"));
      gLayout_-&gt;addWidget(bar_);
  
      hLayout_-&gt;addWidget(button1_);
      hLayout_-&gt;addWidget(button2_);
      hLayout_-&gt;addWidget(button3_);
  
      isStackedLayout_ = false;
  
      resize(480,200);
  
  }
  
  Widget::~Widget() { }
  
  void Widget::layoutHorizontal(){
      QLayout *layout = bar_-&gt;findChild&lt;QLayout *&gt;(tr("currentLayout"));
      layout-&gt;removeWidget(button1_);
      layout-&gt;removeWidget(button2_);
      layout-&gt;removeWidget(button3_);
  
      delete layout;
  
      QHBoxLayout *hLayout_ = new QHBoxLayout(bar_);
      hLayout_-&gt;setObjectName(tr("currentLayout"));
      hLayout_-&gt;addWidget(button1_);
      hLayout_-&gt;addWidget(button2_);
      hLayout_-&gt;addWidget(button3_);
  
      isStackedLayout_ = false;
  
  }
  
  void Widget::layoutVertical(){
      QLayout *layout = bar_-&gt;findChild&lt;QLayout *&gt;(tr("currentLayout"));
      layout-&gt;removeWidget(button1_);
      layout-&gt;removeWidget(button2_);
      layout-&gt;removeWidget(button3_);
      delete layout;
  
      QVBoxLayout *vLayout_ = new QVBoxLayout(bar_);
      vLayout_-&gt;setObjectName(tr("currentLayout"));
  
      vLayout_-&gt;addWidget(button1_);
      vLayout_-&gt;addWidget(button2_);
      vLayout_-&gt;addWidget(button3_);
  
      isStackedLayout_ = false;
  }
  
  void Widget::layoutStacked(){
      QLayout *layout = bar_-&gt;findChild&lt;QLayout *&gt;(tr("currentLayout"));
      layout-&gt;removeWidget(button1_);
      layout-&gt;removeWidget(button2_);
      layout-&gt;removeWidget(button3_);
      delete layout;
  
      QStackedLayout *sLayout_ = new QStackedLayout(bar_);
      sLayout_-&gt;setObjectName(tr("currentLayout"));
  
      sLayout_-&gt;addWidget(button1_);
      sLayout_-&gt;addWidget(button2_);
      sLayout_-&gt;addWidget(button3_);
  
      isStackedLayout_ = true;
  }
  </code></pre>