Search code examples
qt-designerpyside6qt6qlayout

QHBoxLayout with Fixed Width


I've designed the following form using Qt Designer. When the main window is resized, I want to keep the left side layout (the QHBoxLayout) at a fixed size and expand only the right side QLineEdit widget.

enter image description here

By adding a vertical spacer to left QHBoxLayout, I was able to make its height fixed but I'm unable to achieve the same for the width.

When the width of the main window increases, the width of both QHBoxLayout and QLineEdit increases.

How can I make the left side layout to be of fixed size?

enter image description here


Solution

  • I'm able to achieve the required behavior by setting the layoutStretch to (0, 1)

    My main horizontal layout contains two widgets, a vertical layout and a QLineEidt. By setting the layout's stretch factor to (0, 1), we specify that the left vertical layout should take a minimum space and the right QLineEdit should take all the available space.

    enter image description here

    As per Qt documentation:

    https://doc.qt.io/qt-6/layout.html#stretch-factors

    If we apply stretch factors to each widget, they will be laid out in proportion (but never less than their minimum size hint), e.g.

    • If any of the widgets have stretch factors set, with a value greater than zero, then they are allocated space in proportion to their stretch factor (explained below).
    • If any of the widgets have stretch factors set to zero they will only get more space if no other widgets want the space. Of these, space is allocated to widgets with an Expanding size policy first.
    • Any widgets that are allocated less space than their minimum size (or minimum size hint if no minimum size is specified) are allocated this minimum size they require. (Widgets don't have to have a minimum size or minimum size hint in which case the stretch factor is their determining factor.)