9.6.2.  Size Policy and Size Hint

[ fromfile: layout.xml id: sizepolicy ]

Each QWidget has a several attributes that relate to its size on the screen. Some widgets can use extra space at runtime in one or both directions. You can control the default resizing behavior of your custom widget by setting its sizePolicy and sizeHint.

The sizeHint holds the recommended QSize for your widget; i.e., the size that it has when it first appears on the screen. A QSize defines the size of a widget by holding its width and height. There are several member functions that give you complete control of your widget's size at runtime including setMinimumSize(), setMaximumSize(), setMinimumHeight(), setMaximumWidth(), setSizeHint(), etc.

Each QWidget and QLayout can have a horizontal and a vertical QSizePolicy. The size policies, Minimum, Maximum, Fixed, Preferred, Expanding, MinimumExpanding, and Ignore express the willingness of the widget or layout to be resized. The default policy is Preferred/Preferred, which indicates that the sizeHint has the preferred size for the widget. Ignore indicates that minimumHeight and minimumWidth do not constrain resizing operations.

Widgets that display something fixed in size would not benefit at all from extra space given to a resized widget. Therefore, they should have Fixed or Preferred horizontal and vertical size policies. In contrast, scrollable widgets, container widgets, and text editing widgets should have an expanding size policy, so they can display more information to the user, because this is why the user probably made the widget bigger in the first place. Extra space is given or taken from the widgets that have a flexible size policy.

Widgets in the same layout, with expanding policies, are given extra space at a different rate from others, based on stretch factor.

Figure 9.14.  Stretchy Buttons

Stretchy Buttons

In Figure 9.14, you can see that pushbuttons normally do not eat up extra space in either dimension. On the first two rows, some buttons have a horizontal expanding size policy. The second row demonstrates how stretch factors can affect the expanding buttons. On the third row, you can see that one button eats up all of the extra vertical space and forces the first two rows as high up as possible.