1 设置控件尺寸
1.1 查看最大和最小尺寸
- 默认情况下控件的最大值和最小值是没有做限制的;
- 比如拖动一个按钮,看下它的属性:
- 从图中可以看到最小值为0,最大值也有个限制,我们可以在这个范围内对控件进行随意拖动放大和缩小。
1.2 设置最大和最小尺寸
- 通过控件属性编辑器,我们把控件的最小值设为200200,最大值为400400,如下:
- 那这个控件只能在200-400之间进行变化,比如200*200,如下:
- 比如400*400,如下:
2 期望尺寸
2.1 期望尺寸
- 每个控件都有一个默认的推荐尺寸或期望尺寸(
sizeHint
); - 大部分控件,
sizeHint
是只读属性; - 拖两个按钮和文本控件,如下:
- 保存为
test011_sizeHint.ui
,并转为test011_sizeHint.py
:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test011_sizeHint.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(130, 210, 171, 101))
self.textEdit.setObjectName("textEdit")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(130, 110, 194, 30))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.widget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
2.2 获取期望尺寸
- 在
test011_sizeHint.py
代码中加入如下,分别获取三个控件的期望尺寸:
self.控件.sizeHint().height()
self.控件.sizeHint().width()
- 获取
textEdit
期望尺寸:
print(f"获取控件textEdit的期望尺寸的高度:{self.textEdit.sizeHint().height()}")
print(f"获取控件textEdit的期望尺寸的宽度:{self.textEdit.sizeHint().width()}")
- 获取
pushButton
的期望尺寸:
print(f"获取控件pushButton的期望尺寸的高度:{self.pushButton.sizeHint().height()}")
print(f"获取控件pushButton的期望尺寸的宽度:{self.pushButton.sizeHint().width()}")
- 获取
pushButton_2
的期望尺寸:
print(f"获取控件pushButton_2的期望尺寸的高度:{self.pushButton_2.sizeHint().height()}")
print(f"获取控件pushButton_2的期望尺寸的宽度:{self.pushButton_2.sizeHint().width()}")
test011_sizeHint.py
修改后如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test011_sizeHint.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow") s
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
print(f"获取控件textEdit的期望尺寸的高度:{self.textEdit.sizeHint().height()}")
print(f"获取控件textEdit的期望尺寸的宽度:{self.textEdit.sizeHint().width()}")
self.textEdit.setGeometry(QtCore.QRect(130, 210, 171, 101))
self.textEdit.setObjectName("textEdit")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(130, 110, 194, 30))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
print(f"获取控件pushButton的期望尺寸的高度:{self.pushButton.sizeHint().height()}")
print(f"获取控件pushButton的期望尺寸的宽度:{self.pushButton.sizeHint().width()}")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.widget)
self.pushButton_2.setObjectName("pushButton_2")
print(f"获取控件pushButton_2的期望尺寸的高度:{self.pushButton_2.sizeHint().height()}")
print(f"获取控件pushButton_2的期望尺寸的宽度:{self.pushButton_2.sizeHint().width()}")
self.horizontalLayout.addWidget(self.pushButton_2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
- 创建
main.py
调用并执行:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/9/18
# 文件名称:main.py
# 作用:主程序入口
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import sys
from test_case.test011_sizeHint import Ui_MainWindow
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())
- 获取的期望尺寸如下:
获取控件textEdit的期望尺寸的高度:192
获取控件textEdit的期望尺寸的宽度:256
获取控件pushButton的期望尺寸的高度:34
获取控件pushButton的期望尺寸的宽度:47
获取控件pushButton_2的期望尺寸的高度:34
获取控件pushButton_2的期望尺寸的宽度:47
- 大部分控件的最小期望尺寸和期望尺寸是一样的,获取方法为:
self.控件.minimumSize().height()
self.控件.minimumSize().width()
3 sizePolicy(尺寸策略)
3.1 简介
sizePolicy
(尺寸策略)是用来管理部件尺寸的;- 每个部件都有一个大小或尺寸策略,它定义了部件再布局管理中如何分配大小;
- 大小由
QSizePolicy
类实现; - 每个部件基本都有水平策略和垂直策略两种。
3.2 属性值
- 不管是水平策略还是垂直策略,都有以下属性;
属性 | 说明 |
---|---|
Fixed(固定) |
保持固定大小,不会根据可用空间进行调整 |
Minimum(最小) |
尽可能小,满足最小尺寸要求 |
Maximum(最大) |
尽可能大,不会超过最大尺寸要求 |
Preferred(首选) |
尽可能保持首选尺寸,但可根据可用空间进行调整 |
Expanding(扩展) |
尽量扩展以填充可用空间,但不可超过最大尺寸 |
Ignored(忽略) |
布局管理器将忽略部件尺寸的大小 |
- 水平伸展和垂直伸展,主要是来描述可用空间的占用比例的。
4 示例
- 拖动一个水平布局:
- 拖动一个树控件到水平布局,此时会铺满整个布局:
- 再拖动一个面板控件,此时如下:
- 而以上两个部件的水平和垂直策略都是
Expanding
,水平和垂直伸展都是0,各占50%空间; - 我们调整下左边树控件占比三分之一,右边面板控件占比三分之二:
水平和垂直策略都是
Expanding
,树控件的水平和垂直伸展都设置1,面板控件水平和垂直伸展都设置2即可;
- 再在右边的面板控件拖动几个标签和文本,如图:
- 保存为
test012_sizePolicy.ui
,转换成test012_sizePolicy.py
:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test012_sizePolicy.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(9, 9, 781, 551))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.treeView = QtWidgets.QTreeView(self.horizontalLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(1)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.treeView.sizePolicy().hasHeightForWidth())
self.treeView.setSizePolicy(sizePolicy)
self.treeView.setObjectName("treeView")
self.horizontalLayout.addWidget(self.treeView)
self.frame = QtWidgets.QFrame(self.horizontalLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(2)
sizePolicy.setVerticalStretch(2)
sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
self.frame.setSizePolicy(sizePolicy)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.listView = QtWidgets.QListView(self.frame)
self.listView.setGeometry(QtCore.QRect(70, 30, 291, 192))
self.listView.setObjectName("listView")
self.pushButton = QtWidgets.QPushButton(self.frame)
self.pushButton.setGeometry(QtCore.QRect(250, 120, 75, 23))
self.pushButton.setObjectName("pushButton")
self.widget = QtWidgets.QWidget(self.frame)
self.widget.setGeometry(QtCore.QRect(100, 60, 239, 22))
self.widget.setObjectName("widget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.horizontalLayout_2.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.widget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_2.addWidget(self.lineEdit)
self.widget1 = QtWidgets.QWidget(self.frame)
self.widget1.setGeometry(QtCore.QRect(100, 90, 224, 22))
self.widget1.setObjectName("widget1")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.widget1)
self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_2 = QtWidgets.QLabel(self.widget1)
self.label_2.setObjectName("label_2")
self.horizontalLayout_3.addWidget(self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(self.widget1)
self.lineEdit_2.setText("")
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_3.addWidget(self.lineEdit_2)
self.horizontalLayout.addWidget(self.frame)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "登录"))
self.label.setText(_translate("MainWindow", "用户名:"))
self.label_2.setText(_translate("MainWindow", "密码:"))
- 创建
main.py
调用并执行:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/9/18
# 文件名称:main.py
# 作用:主程序入口
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import sys
from test_case.test012_sizePolicy import Ui_MainWindow
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())