MainWindow 小部件调整大小(Pyside) [英] MainWindow Widget Resize (Pyside)

查看:120
本文介绍了MainWindow 小部件调整大小(Pyside)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调整 GUI 应用程序的 MainWindow 大小时遇到​​问题.

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

这是我尝试运行应用程序时看到的:链接图片 1

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

当我尝试用鼠标调整大小时会发生什么:链接图片 2

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

我希望如此,当我尝试调整 MainWindow 的大小时,它会像我之前展示的第一张图片一样显示里面的小部件,而不是每个标签"之间的间距很大.

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

您可以直接使用 setUi()、setGridUI() 函数,忽略其余代码.试着剪掉一些,让它变得简单..

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..

谢谢..

推荐答案

如果您想要调整大小而不修改 QGridLayout 的中心,则需要对周围的一些行进行不同的拉伸.

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

希望有所帮助.

这篇关于MainWindow 小部件调整大小(Pyside)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆