MainWindow小部件调整大小(Pyside)

时间:2014-01-26 21:46:37

标签: python qt resize pyside qmainwindow

我在修改GUI应用程序的MainWindow时出现问题。

这是我在尝试运行应用程序时看到的内容: Link Image 1

当我尝试用鼠标调整大小时会发生什么: Link Image 2

我想,当我尝试调整MainWindow的大小时,它会像我之前显示的第一个图像一样显示Widget,而不是每个“标签”之间有大的间距。

如果它可以帮助这是代码: Link Code

你可以直接使用函数setUi(),setGridUI(),忽略其余的代码。试图削减一些,使其变得简单..

谢谢..

1 个答案:

答案 0 :(得分:2)

如果您希望调整大小不修改QGridLayout的中心,则需要在某些周围的行上放置不同的拉伸。

我在您的内容的上方和下方添加了一行,并在您的内容的左侧和右侧添加了一列,并添加了一段。

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

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

enter image description here

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'}")

希望有所帮助。

相关问题