MainWindow Widget Resize (Pyside)

2019-08-05 00:45发布

问题:

I have a problem with the resize of the MainWindow of a GUI Application.

This is what i see when i try to run the Application: Link Image 1

and what happen when i try to resize it with the mouse: Link Image 2

I would like that, when i try to resize the MainWindow it shows the Widget inside like the first image i showed before instead have that large spacing between each "label".

If it can help this is the code: Link Code

You can go straigth on to the functions setUi(), setGridUI(), ignoring the rest of the code. Tried to cut some of it, to make it simple..

Thank you..

回答1:

If you want a resize to not modify the center of the QGridLayout, you need to put a different stretch on some surrounding rows.

I added a row above and below your content, and a column to the left and right of your content and added a stretch.

http://doc.qt.io/qt-4.8/qgridlayout.html#setRowStretch

http://doc.qt.io/qt-4.8/qgridlayout.html#setColumnStretch

def setupGridUI(self):
    widget = QWidget()
    layout = QGridLayout()
    width, height = 10, 10

    root_x, root_y = random.randrange(width), random.randrange(height)

    for x in range(width):
        for y in range(height):
            random_wall = random.randrange(3)
            if x == root_x and y == root_y:
                label = ClickableLabel(x, y, False, True)
            else:
                if random_wall == 0:
                    label = ClickableLabel(x, y, True)
                else:
                    label = ClickableLabel(x, y)

            layout.addWidget(label, x+1, y+1) # modified

    # added the following 4 lines
    layout.setRowStretch(0, 1);
    layout.setRowStretch(height+2, 1);
    layout.setColumnStretch(0, 1);
    layout.setColumnStretch(width+2, 1);

    widget.setLayout(layout)
    self.setCentralWidget(widget)

    self.setStyleSheet("QMainWindow {background: 'purple'}")

Hope that helps.