MainWindow Widget Resize (Pyside)

≯℡__Kan透↙ 提交于 2019-12-11 11:28:51

问题


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.



来源:https://stackoverflow.com/questions/21369683/mainwindow-widget-resize-pyside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!