Nominate our 2022 Qt Champions!

Layout centering on QStackedWidget

  • I'm writing a program that uses a QTabWidget as its base. To avoid putting everything in the same place, I'm using a QStackedWidget in one of the tabs so I can put several stuff there, which is a good idea for me.
    The problem is, I designed (on Qt Designer) a simple login form, but the problem is I can't seem to be able to center my design on the QStackedWdiget, the layout is always aligned to top-left. I have managed to use a QGridLayout to fake a CSS-like padding design, but I would really like to keep the layout I designed fixed-sized, and it is impossible with QGridLayout, as we all know.
    The question is: how can I have my layout centered in the widget, such that the widget would only fill the surrounds much like we can do in QML with the anchors?

    Thanks in advance.

  • you can use spacers to move your form into the center
    else you can use the alignment parameter in "QBoxLayout::addWidget(QWidget * widget, int stretch = 0, Qt::Alignment alignment = 0)": to align the elements in the center, but you have to layout the form using code and not designer

  • Thanks for the answer, but the main problem is that QStackedWidget treats my widget as the page itself, therefore I can't add a Widget to the layout, since my Widget is the page itself.

  • well, i never used QStackedWidget before, but you can use this turn around, add your widget to a layout and add this layout to an empty widget and use this one to be the page of the QStackedWidget

  • I added a layout to the QBoxLayout, it looks like it works alright this way.
    Thanks for the help.

Log in to reply