PYQT5学习与开发

PyQt5编程(24):在窗口中布局组件—水平对齐和垂直对齐

2018-01-01  本文已影响0人  用电热毯烤猪
容器组件(也称为布局管理器或几何管理器)克服了绝对定位的缺点,在调整窗口大小时,会自动更改添加到容器中的所有组件的特征,包括字体大小,文本长度等。
有两个类用于组件的对齐操作:

QHBoxLayout:水平布局,在水平方向上排列控件,即:左右排列。 构造函数为:
QHBoxLayout([QWidget  parent]),[]表示参数为可选
QVBoxLayout:垂直布局,在垂直方向上排列控件,即:上下排列。构造函数为:
QVBoxLayout([QWidget  parent]),[]表示参数为可选

这两类的继承层次结构如下:
(QObject,QLayoutltem) - QLayout - QBoxLayout - QHBoxLayout
(QObject,QLayoutltem) - QLayout - QBoxLayout - QVBoxLayout
这两个类都不是QWidget类的继承者,因此没有自己的窗口,不能单独使用。 因此,容器作为子控件使用。
可以在QHBoxLayout和QVBoxLayout类的构造函数中指定父组件,也可将容器作参数,调用父组件的setLayout( )方法。
示例:

from PyQt5 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget() # 父窗口
window.setWindowTitle("QHBoxLayout")
window.resize(300, 60)
button1 = QtWidgets.QPushButton("1")
button2 = QtWidgets.QPushButton("2")
hbox = QtWidgets.QHBoxLayout() # 创建容器
hbox.addWidget(button1) # 添加组件
hbox.addWidget(button2)
window.setLayout(hbox) # 指定父组件
window.show()
sys.exit(app.exec_())

相关的方法有:

addWidget( ) -添加组件到容器的末尾,其格式为:
addWidget (Component [, stretch = 0] [, alignment = 0])
参数1:要加入容器的组件
参数2:拉伸方式,可选项
参数3:对齐方式,可选项
参数2,3可按顺序或指定名称设置参数:
hbox.addWidget(button1, 10, QtCore.Qt.AlignRight)
hbox.addWidget(button2, stretch=10)
hbox.addWidget(button3, alignment=QtCore.Qt.AlignRight)

 insertWidget () - 添加组件到容器的指定位置,其格式为:
insertWidget (Index,Component [,stretch = 0] [, alignment = 0])
Index: 0,添加到容器的最前面;-1,添加到容器的末尾。其他参数同addWidget( )。
hbox.addWidget (button1)
hbox.insertWidget (-1, button2) # 添加到容器的末尾
hbox.insertWidget (0, button3) # 添加到容器的最前面
removeWidget(Component) - 从容器中删除组件;
replaceWidget( ) - 替换容器中的组件。格式为:
replaceWidget (component1, component2 [, options= FindChildrenRecursively])
参数1:被替换的组件
参数2:用来替换的组件
参数3:查找和替换方式。可以是QtCore.Qt中的FindDirectChildrenOnly(查找直接子组件)或FindChildrenRecursively( 递归查找子组件)。
addLayout( ) - 将另一个容器添加到当前容器的末尾。 使用这种方法,可以将一个容器放入另一个容器中,从而创建一个复杂的结构。 格式:
addLayout(Container [,stretch = 0])
insertLayout() - 将另一个容器添加到当前容器的指定位置。 如果第一个参数设置为负值,则将容器添加到末尾。 方法格式:
insertLayout(Index,Container [,stretch = 0])
insertSpacing (Index>, Size) - 添加空白间隔到特定位置。间隔的大小以像素为单位。如果第一个参数为负值,那么间隔将被添加到容器的末尾;
addStretch([stretch = 0]) - 将可拉伸空间添加到容器的末端。
insertStretch (Index [, stretch = 0]) - 作用与addStretch( )相似,增加了指定位置的参数。 Index:-1,添加到容器的末尾.

addWidget( )和insertwidget( )方法中的alignment参数用来设置添加到容器中组件的对齐方式。 可以指定QtCore.Qt类的以下属性:

AlignLeft,   1: 水平左对齐
AlignRight, 2: 水平右对齐
AlignHCenter, 4:水平居中
AlignJustify,8: 填满空间
AlignTop,32:垂直上对齐
AlignBottom,64:垂直下对齐
AlignVCenter, 128:垂直居中
AlignBaseiine ,256:垂直基线对齐;
AlignCenter- AlignVCenter|AlignHCenter-垂直、水平均居中;
AlignAbsolute,16:如果通过setLayoutDirection()函数将对齐方式设置为 QtCore.Qt.RightToLeft(默认为QtCore.Qt.LeftToRight),AlignLeft(水平左对齐)实际表现为水平右对齐。要确保AlignLeft(水平左对齐)实际表现为水平左对齐,要和AlignAbsolute同时使用,即:AlignAbsolute|AlignLeft。
setDirection (direction) - 设置容器内组件的排列方式,direction可为QBoxLayout类的枚举变量:
• LeftToRight - 0 - 从左到右(水平容器的默认方式)
• RightToLeft - 1 - 从右到左
• TopToBottom-2 - 从上到下(垂直容器的默认方式)
• BottomToTop - 3 - 从下到上
setContentsMargins () - 设置容器四周的空白边界,格式为:
setContentsMargins (Left,Top,Right, Bottom)
hbox.setContentsMargins (2, 4, 2, 4)
m = QtCore.QMargins (4, 2, 4, 2)
hbox.setContentsMargins (m)
上一篇下一篇

猜你喜欢

热点阅读