Jonathan Liu
Jonathan Liu

Reputation: 93

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),
                (0,2),
                (1,2),
                (2,2),
                (2,1),
                (2,0),
                (1,0),
                (0,0)]

        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)

Result

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?

Upvotes: 2

Views: 9697

Answers (1)

furas
furas

Reputation: 142631

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)

Upvotes: 7

Related Questions