Jonathan Liu Jonathan Liu - 1 year ago 107
Python Question

Why do some widgets not stretch to fill the maximum space in a layout?

When I create a QGridLayout, and I add a couple of widgets to it, some of the widgets do not stretch to fill the section of the layout that contains it.

class Grid(QGridLayout):
def __init__(self, grid_widget):
arrow = QPixmap("icons/arrow.png") #initial image is facing up
self.arrows = []
locs = [(0,1),

for rot in range(len(locs)):
transform = QTransform().rotate(45*rot)
icon = arrow.transformed(transform, Qt.SmoothTransformation)
self.arrows.append(ExpandButton(icon, rot, grid_widget)) #ExpandButton inherits from QPushButton

for i, loc in enumerate(locs):
self.addWidget(self.arrows[i], loc[0], loc[1])

self.addWidget(grid_widget, 1, 1)


As you can see, the QPushButtons on the sides to not stretch to fill the layout like the QPushButtons at the top. Is there any way to fix this so that the QPushButtons at the sides stretch vertically to fill the layout, like how the ones at the top stretch horizontally to fill the layout there?

Answer Source

As I know buttons (as default) expand only horizontally - everybody expect low buttons.

You have to set vertical and horizontal expanding.

button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download