Layouts and the SizePolicy in QT

The starting point to customize your QT layout is the sizeHint attribute. It's a bit mystery because there is no setSizeHint() or similar function, you have to always overload it. Fortunately in Python you can do it in a just one line:

widget.sizeHint = lambda: QSize(100,30)

That's a trivial example. If the size of the widget depends on the size of the content you need to write something more sophisticated.

b1 = QPushButton( "B1" )
b1.sizeHint = lambda: QSize(100,30)
hbox.addWidget( b1 )

b2 = QPushButton( "B2" )
b2.sizeHint = lambda: QSize(200,30)
hbox.addWidget( b2 )

b3 = QPushButton( "B3" )
b3.sizeHint = lambda: QSize(300,30)
hbox.addWidget( b3 )

If the layout will use minimal space (for example a default window size) you will get the 1:2:3 ratio in the widths. But if you just make the window (parent widget) bigger, the size of the buttons will become the same. What is missing is the stretch factor.

Stretch factor takes place where is more space to distribute. It can be set in two ways. By SizePolicy or by addWidget() method.

b1 = QPushButton( "B1" )
b1.sizeHint = lambda: QSize(100,30)
hbox.addWidget( b1, 1 )

b2 = QPushButton( "B2" )
b2.sizeHint = lambda: QSize(200,30)
hbox.addWidget( b2, 2 )

b3 = QPushButton( "B3" )
b3.sizeHint = lambda: QSize(300,30)
hbox.addWidget( b3, 3 )

Remember, only stretch factor sets proportions between widget sizes, not sizeHint. The sizHint and minimumSizeHint are just hints :)

When you have a sizeHint ready that's a half way. Second and mandatory step is to set up a SizePolicy. It was confusing to me, not easy visible in the documentation, that SizePolicy describes just sizeHint values. Nothing more.

b1 = QPushButton( "B1" )
b1.sizeHint = lambda: QSize(100,30)
p1 = QSizePolicy( QSizePolicy.Maximum, QSizePolicy.Fixed )
p1.setHorizontalStretch( 1 )
b1.setSizePolicy( p1 )
hbox.addWidget( b1 )

b2 = QPushButton( "B2" )
b2.sizeHint = lambda: QSize(200,30)
p2 = QSizePolicy( QSizePolicy.Minimum, QSizePolicy.Fixed )
p2.setHorizontalStretch( 2 )
b2.setSizePolicy( p2 )
hbox.addWidget( b2 )

b3 = QPushButton("B3")
b3.sizeHint = lambda: QSize(300,30)
p3 = QSizePolicy( QSizePolicy.Preferred, QSizePolicy.Fixed )
p3.setHorizontalStretch( 3 )
b3.setSizePolicy( p3 )
hbox.addWidget( b3 )

Please note that words like Maximum and Minimum tell us if the sizeHint value is Maximal or Minimal.

If you get The Size Policy Documentation right you should be ready to go.