Qt-UI

Layout Policies

Widgets and Layouts | | GUI Elements

The particular graphical appearance of a layout is defined by the size policies of the owned widgets, unless the policy of the layout does not specify a different behavior.

Each widget has a horizontal/vertical layout policy (QSizePolicy): The default policy is Preferred/Preferred, which means that the widget can be freely resized, but prefers to be the size sizeHint() returns. Button-like widgets set the size policy to specify that they may stretch horizontally, but are fixed vertically. Additional policies are:

  • expanding direction: vertical or horizontal or both
  • item alignment : left, right, centered
  • spacing between items
  • stretch: how big a item can grow relative to others to accomodate the canvans size
  • margins : border size around each item
  • etc.

Predefined QHBoxLayout and QVBoxLayout for linear alignment:

  • QHBoxLayout organizes the contained items horizontally beneath each other.
  • QVBoxLayout organizes the contained items vertically below each other.

Additional control via special items added to a layout:

  • l→addSpacerItem()
    • add fixed space between two items
  • l→addStretch(int stretch)
    • add growable space between two items to accomodate to the available canvas size

Widgets are normally created without any stretch factor set. When they are laid out in a layout the widgets are given a share of space in accordance with their QWidget::sizePolicy() or their minimum size hint whichever is the greater. Stretch factors are used to change how much space widgets are given in proportion to one another.

Widgets and Layouts | | GUI Elements

Options: