如何阻止自定义小部件在 PyQt5 中扩展? [英] How do I stop a custom widget from expanding in PyQt5?

查看:50
本文介绍了如何阻止自定义小部件在 PyQt5 中扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个包含许多其他小部件的小部件,但我一直在调整窗口大小时遇到​​问题:即使我告诉"小部件,小部件也会不断扩展.它不是.这是我的最小示例:

I'm trying to make a widget which contains many other widgets and I keep having problems with resizing the window: the widget keeps expanding even if I "tell" it not to. Here is my minimal example:

from PyQt5 import QtWidgets, QtGui, QtCore

class CustomWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.layout  = QtWidgets.QGridLayout()
        self.button1 = QtWidgets.QPushButton("Button A")
        self.button2 = QtWidgets.QPushButton("Button B")
        self.label1  = QtWidgets.QLabel("Long label that can span multiple columns")

        self.layout.addWidget(self.button1, 0, 0)
        self.layout.addWidget(self.button2, 0, 1)
        self.layout.addWidget(self.label1, 1, 0, 1, 2)
        self.setLayout(self.layout)


class App(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.cw = CustomWidget()
        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.cw)

        self.setLayout(self.layout)
        self.show()

QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create("Fusion"))
app = QtWidgets.QApplication([])
win = App()
status = app.exec_()

这段代码确实有效,但是如果我调整窗口大小,那么所有按钮和标签都会散布在屏幕上,这不是我想要的.

This code does work however if I resize the window then all the buttons and labels get spread out over the screen which is not what I want.

我试过了:

  • 设置固定大小:不起作用,因为标签文本的长度可能不同,我希望小部件相应地调整大小(但尽可能小)

  • Setting a fixed size: doesn't work because the label text can be different lengths and I want the widget to resize accordingly (but stay as small as possible)

self.setFixedSize(self.size()) 不做任何事情,有时会变得更糟

self.setFixedSize(self.size()) doesn't do anything and sometimes makes it worse

self.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum) 或任何其他大小策略似乎什么都不做

self.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum) or any other size policy seems to do nothing

TL;DR 即使有空白空间,我也希望我的小部件缩小,但我不想设置固定大小.

TL;DR I want my widget to shrink even if there's empty space but I don't want to set a fixed size.

通过将 alignment=QtCore.Qt.AlignLeft 传递给所有 self.layout.addWidget 调用,我已经部分解决了这个问题.它并没有完全解决问题,但它可能是朝着正确方向迈出的一步.

I have partially solved the problem by passing in alignment=QtCore.Qt.AlignLeft to all the self.layout.addWidget calls. It doesn't totally get rid of the problem however it may be a step in the right direction.

推荐答案

如果你想让这些小部件尽可能小,你可以向布局添加行和列拉伸,设置为大于使用的最右下角的布局坐标.

If you want to keep those widgets as small as possible, you can add a row and column stretch to the layout, set for a row/column index greater than the most bottom-right layout coordinate used.

在您的情况下,您有两行两列,因此设置第三行和第三列的拉伸就足够了:

In your case, you have two rows and two columns, so it's enough to set the stretch for the third row and column:

    self.layout.setColumnStretch(2, 1)
    self.layout.setRowStretch(2, 1)

显然,您可以将其设置为非常高的索引,这样如果您必须添加更多小部件,您就不必关心它.

Obviously, you can set it for a very high index, so that if you have to add more widgets you don't have to care about it.

如果您想将这些小部件保留在中心,只需从第二行和第二列开始添加它们,并为第一行/列设置行/列拉伸:

If you want to keep those widgets in the center, just add them starting from the second row and column and set the row/column stretch for the first row/column too:

    self.layout.setColumnStretch(0, 1)
    self.layout.setRowStretch(0, 1)
    self.layout.addWidget(self.button1, 1, 1)
    self.layout.addWidget(self.button2, 1, 2)
    self.layout.addWidget(self.label1, 2, 1, 1, 2)
    self.layout.setColumnStretch(3, 1)
    self.layout.setRowStretch(3, 1)

请注意,您也可以使用大小策略,但您必须使用Maximum,而不是Minimum,并且您还需要添加具有正确对齐方式的小部件.

Note that you can also use the size policy, but you have to use Maximum, not Minimum, and you also need to add widgets with the correct alignment.

这篇关于如何阻止自定义小部件在 PyQt5 中扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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