PyQt 5:如何在扩展 window 时保持相对小部件大小
PyQt 5: How to maintain relative widget size when expanding a window
我正在 Maya 2018 中开发自定义 ui,它使用 PyQt5(通过 PySide2 - 存在一些细微差别,但它本质上是 PyQt5)。
我有一个 QHBoxLayout
,里面有两个小部件,它们的大小调整策略都设置为 'expanding'。我将其中一个的最小宽度设置为另一个的两倍左右。
每当我展开 window 时,较小的小部件就会展开,直到它与较大的小部件大小相同(根本不会改变大小)...然后 然后 他们都以同样的速度继续扩张——都占据了 window.
的一半
我希望它们在整个扩展过程中保持彼此的相对大小。
有没有一种我完全忽略的方法可以做到这一点?
这是我为重现问题而编写的一些简化代码:
import PySide2.QtWidgets as QtWidgets
class TestDialog(QtWidgets.QDialog):
def __init__(self, *args, **kwargs):
QtWidgets.QDialog.__init__(self, *args, **kwargs)
main_layout = QtWidgets.QHBoxLayout()
self.setLayout(main_layout)
main_layout.setContentsMargins(5, 5, 5, 5)
main_layout.setSpacing(5)
w1 = QtWidgets.QPushButton('small')
w2 = QtWidgets.QPushButton('large')
w1.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
w2.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
w2.setMinimumWidth(250)
main_layout.addWidget(w1)
main_layout.addWidget(w2)
self.setMinimumWidth(450)
self.setMinimumHeight(100)
self.resize(450, 100)
test = TestDialog()
test.show()
如果您希望保持宽度之间的关系,则必须通过添加小部件来设置拉伸:
from PySide2 import QtWidgets
class TestDialog(QtWidgets.QDialog):
def __init__(self, *args, **kwargs):
super(TestDialog, self).__init__(*args, **kwargs)
main_layout = QtWidgets.QHBoxLayout(self)
main_layout.setContentsMargins(5, 5, 5, 5)
main_layout.setSpacing(5)
w1 = QtWidgets.QPushButton("small")
w2 = QtWidgets.QPushButton("large")
w1.setSizePolicy(
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
)
w2.setSizePolicy(
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
)
main_layout.addWidget(w1, <b>stretch=1</b>)
main_layout.addWidget(w2, <b>stretch=2</b>)
self.setMinimumSize(450, 100)
self.resize(450, 100)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
test = TestDialog()
test.show()
sys.exit(app.exec_())
我正在 Maya 2018 中开发自定义 ui,它使用 PyQt5(通过 PySide2 - 存在一些细微差别,但它本质上是 PyQt5)。
我有一个 QHBoxLayout
,里面有两个小部件,它们的大小调整策略都设置为 'expanding'。我将其中一个的最小宽度设置为另一个的两倍左右。
每当我展开 window 时,较小的小部件就会展开,直到它与较大的小部件大小相同(根本不会改变大小)...然后 然后 他们都以同样的速度继续扩张——都占据了 window.
的一半我希望它们在整个扩展过程中保持彼此的相对大小。
有没有一种我完全忽略的方法可以做到这一点?
这是我为重现问题而编写的一些简化代码:
import PySide2.QtWidgets as QtWidgets
class TestDialog(QtWidgets.QDialog):
def __init__(self, *args, **kwargs):
QtWidgets.QDialog.__init__(self, *args, **kwargs)
main_layout = QtWidgets.QHBoxLayout()
self.setLayout(main_layout)
main_layout.setContentsMargins(5, 5, 5, 5)
main_layout.setSpacing(5)
w1 = QtWidgets.QPushButton('small')
w2 = QtWidgets.QPushButton('large')
w1.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
w2.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
w2.setMinimumWidth(250)
main_layout.addWidget(w1)
main_layout.addWidget(w2)
self.setMinimumWidth(450)
self.setMinimumHeight(100)
self.resize(450, 100)
test = TestDialog()
test.show()
如果您希望保持宽度之间的关系,则必须通过添加小部件来设置拉伸:
from PySide2 import QtWidgets
class TestDialog(QtWidgets.QDialog):
def __init__(self, *args, **kwargs):
super(TestDialog, self).__init__(*args, **kwargs)
main_layout = QtWidgets.QHBoxLayout(self)
main_layout.setContentsMargins(5, 5, 5, 5)
main_layout.setSpacing(5)
w1 = QtWidgets.QPushButton("small")
w2 = QtWidgets.QPushButton("large")
w1.setSizePolicy(
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
)
w2.setSizePolicy(
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
)
main_layout.addWidget(w1, <b>stretch=1</b>)
main_layout.addWidget(w2, <b>stretch=2</b>)
self.setMinimumSize(450, 100)
self.resize(450, 100)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
test = TestDialog()
test.show()
sys.exit(app.exec_())