How to move a widget from a layout to another layout?
- 
hello all!! im new in Qt and C++, I want to move 2 widget from a vertical layout to horizontal layout when a button is clicked. I already tried two days in a row and dont know how to solve that problem... (I already designed the UI and a sliding menu writing in C++) example: 
 when I click the burger button I want those two button below the burger button move to the same layout as the burger button. after clicking  Thanks you!! 
- 
hello all!! im new in Qt and C++, I want to move 2 widget from a vertical layout to horizontal layout when a button is clicked. I already tried two days in a row and dont know how to solve that problem... (I already designed the UI and a sliding menu writing in C++) example: 
 when I click the burger button I want those two button below the burger button move to the same layout as the burger button. after clicking  Thanks you!! @HelloWorld88 Find out the index of the widget you want to move in the current layout using https://doc.qt.io/qt-5/qlayout.html#indexOf 
 Then remove the widget from the current layout using https://doc.qt.io/qt-5/qlayout.html#takeAt
 Then add the widget to other layout using https://doc.qt.io/qt-5/qlayout.html#addWidget
- 
@HelloWorld88 Find out the index of the widget you want to move in the current layout using https://doc.qt.io/qt-5/qlayout.html#indexOf 
 Then remove the widget from the current layout using https://doc.qt.io/qt-5/qlayout.html#takeAt
 Then add the widget to other layout using https://doc.qt.io/qt-5/qlayout.html#addWidget
- 
@jsulm 
 I think you can skipindexOf()+takeAt()by using https://doc.qt.io/qt-5/qlayout.html#removeWidget instead? (Though you may get no warning if the widget isn't found, whereindexOf()would have let you know.)
- 
@jsulm 
 I think you can skipindexOf()+takeAt()by using https://doc.qt.io/qt-5/qlayout.html#removeWidget instead? (Though you may get no warning if the widget isn't found, whereindexOf()would have let you know.)
- 
Hi, Where is the stretch located ? Based on that you can use the takeAt method. 
- 
@JonB @jsulm You don't need takeAtnorremoveWidget. Every widget can only be a child of one parent. If you useaddWidgetto add the widget to a different layout it will be reparented and thus be removed from the previous layout.@HelloWorld88 Removing the stretch is a little bit more complicated. How did you add it? Did you create your layout using ui-files or did you assemble your widgets in pure C++ code? Did you use addStretch()to add the stretch? Maybe @SGaist 's method already works.
- 
@JonB @jsulm You don't need takeAtnorremoveWidget. Every widget can only be a child of one parent. If you useaddWidgetto add the widget to a different layout it will be reparented and thus be removed from the previous layout.@HelloWorld88 Removing the stretch is a little bit more complicated. How did you add it? Did you create your layout using ui-files or did you assemble your widgets in pure C++ code? Did you use addStretch()to add the stretch? Maybe @SGaist 's method already works.@SimonSchroeder 
 Interesting! Seems a little bit dodgy to me --- I'd be fearful of getting a warning that the widget was already added to some other layout and I should take/remove it first! But I respect what you say.Since the OP announced he now wants to move a stretch and not just a widget, I would have thought he will want to use indexOf()to locate the widget and then from that locate the stretch as the previous/next sibling to remove, or similar.
- 
@JonB @jsulm @SGaist @SimonSchroeder thanks you all for you replies I already solve the problem yesterday of the stretch, I used TakeAt method. QLayoutItem *stretch; stretch = mainMenuVLayout->takeAt(3); delete stretch;
- 
@JonB @jsulm @SGaist @SimonSchroeder thanks you all for you replies I already solve the problem yesterday of the stretch, I used TakeAt method. QLayoutItem *stretch; stretch = mainMenuVLayout->takeAt(3); delete stretch;@HelloWorld88 said in How to move a widget from a layout to another layout?: I already solve the problem yesterday of the stretch Thank you for sharing. Don't forget to mark your answer as correct. 
 

