Home / Guides / Layouts

PyQt Layouts: Arrange Widgets the Right Way

Master PyQt layouts — QVBoxLayout, QHBoxLayout, QGridLayout and QFormLayout — with examples, nesting, stretch and spacing so your UI resizes cleanly.

LAYOUTS

PyQt Layouts: Arrange Widgets the Right Way

Layouts are what make a PyQt window look professional and resize gracefully. This guide covers the four you will use every day, plus nesting, stretch and spacing to polish any screen.

Why layouts beat fixed positions

You could place every widget with move(x, y). But that breaks the moment the window resizes or the font changes. Buttons overlap, labels get cut off, and the screen looks wrong on another computer.

Layout managers fix this. They position widgets for you and adjust them when anything changes. Your screen stays tidy at any size. Once you use layouts, you never go back.

Box layouts

Box layouts are the workhorses. QVBoxLayout stacks widgets top to bottom. QHBoxLayout places them in a row.

box.py
from PyQt6.QtWidgets import QVBoxLayout, QPushButton
lay = QVBoxLayout(window)
lay.addWidget(QPushButton("Top"))
lay.addWidget(QPushButton("Bottom"))

Mix the two and you can build most screens. A row of buttons inside a vertical stack covers a surprising number of designs.

Grid layout

QGridLayout arranges widgets in rows and columns. It is perfect for forms, calculators and anything that lines up in a table shape:

grid.py
from PyQt6.QtWidgets import QGridLayout, QPushButton
g = QGridLayout(window)
g.addWidget(QPushButton("1"), 0, 0)
g.addWidget(QPushButton("2"), 0, 1)
g.addWidget(QPushButton("3"), 1, 0, 1, 2)  # span 2 columns

The last two numbers let a widget span several rows or columns. That makes room for wide buttons or headers.

Form layout

QFormLayout pairs a label with an input on each row. It is the fastest way to build settings and sign-up screens:

form.py
from PyQt6.QtWidgets import QFormLayout, QLineEdit
f = QFormLayout(window)
f.addRow("Name:", QLineEdit())
f.addRow("Email:", QLineEdit())

Nesting layouts

Real screens combine layouts. You put one layout inside another with addLayout. For example, a vertical layout for the page, with a horizontal layout at the bottom for the buttons.

Nesting is how you build toolbars, sidebars and split views. Start simple and add one layer at a time.

Stretch, spacing and margins

Three small tools make layouts look finished. addStretch() pushes widgets apart, so you can shove a button to the right. setSpacing() controls the gap between widgets. setContentsMargins() sets the padding around the edges.

Qt Designer builds all of these visually, then you load the result in Python. See our Qt Designer guide.

Size policies explained

Layouts decide where widgets go, but size policies decide how they grow. Each widget can prefer to stay its natural size, or to stretch and fill the space it is given. Setting the right policy is what makes a screen feel balanced.

For example, a text box usually wants to stretch across a row, while a button next to it keeps its natural width. Tell each widget how greedy it should be, and the layout does the rest.

A real two-panel layout

Many apps use a sidebar on the left and a main area on the right. You build this with a horizontal layout holding two vertical layouts. Give the main area a stretch factor so it takes the extra width.

This simple structure scales to real tools: a file list beside an editor, a menu beside a dashboard, settings beside a preview. Once you can build it, a huge range of apps is within reach.

Layout habits that help

A few habits keep layouts tidy. Always use a layout rather than fixed positions. Nest small layouts instead of forcing one giant grid. And add stretches to control empty space on purpose, rather than leaving gaps to chance.

Test your window by resizing it. If everything stays sensible as it shrinks and grows, your layout is doing its job.

Common layout mistakes

A few mistakes trip up newcomers again and again. The biggest is placing widgets with fixed coordinates instead of a layout. It looks fine on your screen, then breaks on a different size or font. Always reach for a layout first.

Another is forgetting to add a widget to the layout at all. It simply does not show up, which is confusing until you spot the missing line. And a third is cramming everything into one giant grid when nested box layouts would be clearer.

Catch these early by resizing your window as you build. If anything jumps, overlaps or vanishes, a layout issue is usually the cause, and the fix is almost always to let a layout do the positioning.

Making screens feel responsive

A responsive window adjusts gracefully as it grows and shrinks. Layouts handle most of this for free, but size policies and stretch factors add the finishing touch. Tell the main area to stretch and the sidebar to stay put, and the window feels intentional.

Add stretches to absorb spare space where you want it, so controls do not drift to awkward spots. Set sensible minimum sizes so a window can never collapse into an unusable sliver.

The payoff is an app that looks right on a laptop and on a large monitor alike, with no extra code per screen size. That adaptability is one of Qt's quiet strengths.

Key takeaways

Layouts are what separate a hobby window from a polished app. Use box layouts for stacks and rows, grid layouts for tables of controls, and form layouts for label-and-input pairs. Nest them for bigger screens like a sidebar beside a main area.

Control the finer feel with size policies, stretch factors, spacing and margins. Tell each widget how greedy it should be, push space where you want it, and set sensible minimum sizes so a window never collapses into a sliver.

Test by resizing. If everything stays sensible as the window grows and shrinks, your layout is doing its job. That adaptability, built once, works on every screen size with no extra code.

FAQs

Which layout should I start with?

Start with box layouts. They handle most screens and are the easiest to picture.

How do I push a widget to the right?

Add a stretch before it with addStretch(). The stretch eats the free space and shifts the widget over.

Can I change a layout after it is set?

Yes. You can add and remove widgets at runtime, which is handy for dynamic screens.

Why do my widgets overlap?

You probably used fixed positions instead of a layout. Switch to a layout and the overlap disappears.

Conclusion

Layouts are the difference between a hobby window and a polished app. Learn the box, grid and form layouts, nest them for bigger screens, and use stretch and spacing to finish the look. Practise by rebuilding the screen in our first example, then wire it up with signals and slots.

Ready to start? Download PyQt or run pip install PyQt6.