我读到“首选策略是非常灵活的策略,小部件的大小可以小于或大于 sizeHint()。”
但是当我增加或减少窗口的大小时,我找不到它们之间的任何差异。
< QSizePolicy.Minimum >
from PySide2 import QtWidgets
app = QtWidgets.QApplication([])
window = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
vMinimumButton = QtWidgets.QPushButton("h_Minimum, v_Fixed")
hMinimumButton = QtWidgets.QPushButton("h_Fixed, v_Minimum")
bMinimumButton = QtWidgets.QPushButton("h_Minimum, v_Minimum")
vMinimumButton.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
hMinimumButton.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Minimum)
bMinimumButton.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
layout.addWidget(vMinimumButton)
layout.addWidget(hMinimumButton)
layout.addWidget(bMinimumButton)
window.setLayout(layout)
window.show()
app.exec_()
< QSizePolicy.Preferred >
from PySide2 import QtWidgets
app = QtWidgets.QApplication([])
window = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
vPreferredButton = QtWidgets.QPushButton("h_Preferred, v_Fixed")
hPreferredButton = QtWidgets.QPushButton("h_Fixed, v_Preferred")
bPreferredButton = QtWidgets.QPushButton("h_Preferred, v_Preferred")
vPreferredButton.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
hPreferredButton.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Preferred)
bPreferredButton.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
layout.addWidget(vPreferredButton)
layout.addWidget(hPreferredButton)
layout.addWidget(bPreferredButton)
window.setLayout(layout)
window.show()
app.exec_()