我在调整图形用户界面应用程序的MainWindow大小方面有问题。
这就是我尝试运行应用程序时所看到的:链接图像1
当我尝试用鼠标调整它的大小时会发生什么:链接图像2
我想这样做,当我试图调整MainWindow的大小时,它会显示内部的Widget,就像我之前显示的第一个图像,而在每个“标签”之间有很大的间距。
如果可以的话,这是代码:链接码
您可以直接转到函数setUi()、setGridUI(),忽略其余的代码。试着剪掉一些,让它变得简单..。
谢谢。。
发布于 2014-01-26 22:58:42
如果希望调整大小而不修改QGridLayout的中心,则需要在周围的一些行上放置不同的长度。
我在内容的上方和下面添加了一行,在内容的左边和右边添加了一列,并添加了一个拉伸。
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'}")希望这能有所帮助。
https://stackoverflow.com/questions/21369683
复制相似问题