Search code examples
qtlayoutqlayout

How to put a button inside another one using layouts


I'm trying to place a button inside/over another one which is feasible when not using any layout, but in my case I'm obliged to use layouts which causes the problem. The pictures below will make it clearer.

Without layout:

enter image description here

With layout:

enter image description here


Solution

  • For this case you can use a QStackedLayout, and set the stack mode to StackAll to allow all widgets to be visible. In C++ you can stackLayout->setStackingMode(QStackedLayout::StackAll);.

    Then, insert each button on a different page, and set the page with the x button as the active page (the front most). Each page can have its own layout, (for example, to place the x button on the top-right corner).

    QStackedLayout is not directly available in the Designer but programmatically only. Nevertheless, you can use the QStackedWidget (a QWidget that uses QStackedLayout) if you want to layout it in Designer. The stackingMode property has to be set programmatically.