Layout Policies

In earlier chapters, we saw many examples of PyQt's layout managers in action. It is possible in PyQt to set specific fixed sizes and positions for widgets, or to handle layouts manually by reimplementing each widget's resizeEvent() handler. But using layout managers is by far the easiest approach, and it offers additional benefits compared with manual approaches.

Layout managers allow widgets to grow and shrink to make the best use of the space available to them, dynamically responding to the user changing the containing form's size. Layout managers provide a minimum size for a form based on all the widgets' minimum sizes. This ensures that the form cannot be made too small to be usable, and is not fixed, but dependent on the widgets' contents—for example, a label might need more or less width depending on whether the text it is displaying is in English or German.

The QVBoxLayout, QHBoxLayout, and QGridLayout layout managers are very versatile. The box layouts can include "stretches" that consume space between widgets to prevent widgets from growing too tall or too wide. And grid layouts can have widgets that span multiple rows and columns. All the layout managers can be nested inside each other, so very sophisticated layouts can be created.

Nonetheless, sometimes the layout managers alone are not sufficient to achieve the layout we want. One simple way to help the layout managers is to

Table 9.1 PyQt's Size Policies

Policy Effect

The widget has the size specified by its size hint and never changes size

The widget's size hint is its minimum size; it cannot be shrunk smaller than this, but it can grow bigger

The widget's size hint is its maximum size; it cannot grow bigger than this, but it can shrink down to its minimum size hint

The widget's size hint is its preferred size; it can be shrunk down to its minimum size hint, or it can grow bigger than its size hint

The widget can be shrunk down to its minimum size hint, or it can grow bigger than its size hint, but it prefers to grow bigger set the size policies of those widgets that are not being laid out satisfactorily. Every widget has vertical and horizontal size policies that can be set independently. (Every widget can also have a fixed minimum and maximum size, but using size policies usually provides better resizing behavior.) In addition, two sizes are associated with every widget: a size hint and a minimum size hint. The former is the widget's preferred size, and the latter is the smallest size the widget can be shrunk to. The sizes are used by the size policies as shown in Table 9.1.

For example, a QLineEdit might have a default horizontal policy of Expanding and a vertical policy of Fixed. This would mean that the line edit will take up as much horizontal space as it can get, but will always have the same vertical size. Every built-in PyQt widget has sensible size hints and size policies already set, so normally we need to change them for only one or two widgets when tweaking a layout.

Size policies also store a "stretch factor" in addition to a policy. This is used to indicate how layout managers should share space between widgets. For example, if we had a QVBoxLayout that contained two QListWidgets, both would want to grow in both directions. But if we wanted the bottom one to grow faster than the top one, we could give the top one a stretch factor of 1 and the bottom one a stretch factor of 3. This will ensure that if the user resizes, the extra space will be distributed between the two widgets in a proportion of 1:3.

If setting size policies and stretch factors is still not enough, we can always create a subclass and reimplement the sizeHint() and minimumSizeHint() methods to return the size we want. We will see examples of this in Chapter 11.

Fixed I I Minimum



Expanding d a

EZf sf

Was this article helpful?

0 -1


The main focus of this report is to show how to get involved in video marketing on the run, how to rank quickly on YouTube and Google using FREE semi-automatic tools and services. QUICKLY AND FREE. I will show methods and techniques I use to rank my videos, as well as free resources and tools to make video clips, to get backlinks and free traffic.

Get My Free Ebook

Post a comment