当前位置:网站首页>PyQt5快速开发与实战 6.4 QBoxLayout(框布局)
PyQt5快速开发与实战 6.4 QBoxLayout(框布局)
2022-07-29 09:25:00 【Ding Jiaxiong】
PyQt5快速开发与实战
文章目录
6. 第6章 PyQt5 布局管理
6.4 QBoxLayout(框布局)
采用QBoxLayout类可以在水平和垂直方向上排列控件,QHBoxLayout和QVBoxLayout类继承自 QBoxLayout类。
6.4.1 QHBoxLayout(水平布局)
采用QHBoxLayout 类,按照从左到右的顺序来添加控件。
QHBoxLayout类中的常用方法:
| 方法 | 描述 |
|---|---|
| addLayout(self , QLayout , stretch = 0 ) | 在窗口的右边添加布局,使用stretch(伸缩量)进行伸缩,伸缩量默认为0 |
| addWidget(self , QWidget , stretch , Qt.Alignment alignment) | 在布局中添加控件:stretch(伸缩量),只适用于QBoxLayout,控件和窗口会随着伸缩量的变大而增大; |
| alignment,指定对齐的方式 | |
| addSpacing(self , int) | 设置各控件的上下间距,通过该方法可以增加额外的空间 |
QHBoxLayout类的继承结构:
QObject → QLayout → QBoxLayout → QHBoxLayout
QHBoxLayout布局时用到的对齐方式参数:
| 参数 | 描述 |
|---|---|
| Qt.AlignLeft | 水平方向居左对齐 |
| Qt.AlignRight | 水平方向居右对齐 |
| Qt.AlignCenter | 水平方向居中对齐 |
| Qt.AlignJustify | 水平方向两端对齐 |
| Qt.AlignTop | 垂直方向靠上对齐 |
| Qt.AlignBottom | 垂直方向靠下对齐 |
| Qt.AlignVCenter | 垂直方向居中对齐 |
水平布局案例
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
class Winform(QWidget):
def __init__(self, parent=None):
super(Winform, self).__init__(parent)
self.setWindowTitle("水平布局管理例子")
# 水平布局按照从左到右的顺序进行添加按钮部件。
hlayout = QHBoxLayout()
hlayout.addWidget(QPushButton(str(1)))
hlayout.addWidget(QPushButton(str(2)))
hlayout.addWidget(QPushButton(str(3)))
hlayout.addWidget(QPushButton(str(4)))
hlayout.addWidget(QPushButton(str(5)))
self.setLayout(hlayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
from PyQt5.QtCore import Qt
class Winform(QWidget):
def __init__(self, parent=None):
super(Winform, self).__init__(parent)
self.setWindowTitle("水平布局管理例子")
self.resize(800, 200)
# 水平布局按照从左到右的顺序进行添加按钮部件。
hlayout = QHBoxLayout()
# 水平居左 垂直居上
hlayout.addWidget(QPushButton(str(1)), 0, Qt.AlignLeft | Qt.AlignTop)
hlayout.addWidget(QPushButton(str(2)), 0, Qt.AlignLeft | Qt.AlignTop)
hlayout.addWidget(QPushButton(str(3)))
# 水平居左 垂直居下
hlayout.addWidget(QPushButton(str(4)), 0, Qt.AlignLeft | Qt.AlignBottom)
hlayout.addWidget(QPushButton(str(5)), 0, Qt.AlignLeft | Qt.AlignBottom)
self.setLayout(hlayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())

还可以使用setSpacing(int)设置各控件之间的间距。

6.4.2 QVBoxLayout(垂直布局)
采用QVBoxLayout类,按照从上到下的顺序添加控件。
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
class Winform(QWidget):
def __init__(self, parent=None):
super(Winform, self).__init__(parent)
self.setWindowTitle("垂直布局管理例子")
self.resize(330, 150)
# 垂直布局按照从上到下的顺序进行添加按钮部件。
vlayout = QVBoxLayout()
vlayout.addWidget(QPushButton(str(1)))
vlayout.addWidget(QPushButton(str(2)))
vlayout.addWidget(QPushButton(str(3)))
vlayout.addWidget(QPushButton(str(4)))
vlayout.addWidget(QPushButton(str(5)))
self.setLayout(vlayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())

6.4.3 addStrech()函数的使用
在布局时要用到addStretch()函数。设置stretch伸缩量后,按比例分配剩余空间。
| 函数 | 描述 |
|---|---|
| QBoxLayout.addStretch(int stretch = 0) | addStretch()函数在布局管理器中增加一个可伸缩的控件(QSpaceltem),0为最小值,并且将 stretch 作为伸缩量添加到布局末尾;stretch参数表示均分的比例,默认值为0 |

上图为不设置伸缩时的样子
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton
import sys
class WindowDemo(QWidget):
def __init__(self):
super().__init__()
btn1 = QPushButton(self)
btn2 = QPushButton(self)
btn3 = QPushButton(self)
btn1.setText('button 1')
btn2.setText('button 2')
btn3.setText('button 3')
hbox = QHBoxLayout()
# 设置伸缩量为1
# hbox.addStretch(1)
hbox.addWidget(btn1)
# 设置伸缩量为1
# hbox.addStretch(1)
hbox.addWidget(btn2)
# 设置伸缩量为1
# hbox.addStretch(1)
hbox.addWidget(btn3)
# 设置伸缩量为1
hbox.addStretch(1)
self.setLayout(hbox)
self.setWindowTitle("addStretch 例子")
self.resize(600,50)
if __name__ == "__main__":
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = WindowDemo()
win.show()
sys.exit(app.exec_())
设置伸缩后

在第一个控件之前添加伸缩控件,所有控件都会居右显示

同理,在最后一个控件之后添加伸缩控件,所有控件都会居左显示

边栏推荐
- 原型链继承和构造函数继承的 “毛病”
- How to introduce your project experience?
- Rocky基础之编译安装apache
- 用户身份标识与账号体系实践
- First order traversal / second order traversal determines the approximate shape of the tree
- Leetcode:132. split palindrome string II
- Axurerp prototype design starts quickly
- C# 使用数据库对ListView控件数据绑定
- Notes on network principles (five layer network)
- The biggest upgrade of Bluetooth over the years: Bluetooth Le audio is about to appear in all kinds of digital products
猜你喜欢
随机推荐
mysql将部分表名统一转换为大写
【Unity入门计划】C#与Unity-了解类和对象
What should I pay attention to now? Excuse me, is it safe to open a stock account by mobile phone?
First order traversal / second order traversal determines the approximate shape of the tree
One article tells you the salary after passing the PMP Exam
Solve the problem of reading data garbled by redis visualization tool
How to change MySQL into Chinese
文件重命名后,怎样将新旧文件名及所在位置导出到excel
Tesseract图文识别--简单
The biggest upgrade of Bluetooth over the years: Bluetooth Le audio is about to appear in all kinds of digital products
MySQL converts some table names to uppercase
Asp graduation project - based on C # +asp Net+sqlserver laboratory reservation system design and Implementation (graduation thesis + program source code) - Laboratory Reservation System
使用cpolar发布树莓派网页(cpolar功能的完善)
C # use database to bind listview control data
36. JS动画
Compile and install Apache for rocky Foundation
Flowable UI production flow chart
"Defects" of prototype chain inheritance and constructor inheritance
数仓项目踩坑记录与解决方法总结
浅谈契约测试









