容器組件(也稱為布局管理器或幾何管理器)克服了絕對(duì)定位的缺點(diǎn),在調(diào)整窗口大小時(shí),會(huì)自動(dòng)更改添加到容器中的所有組件的特征葛假,包括字體大小,文本長度等绵估。
有兩個(gè)類用于組件的對(duì)齊操作:
QHBoxLayout:水平布局,在水平方向上排列控件,即:左右排列。 構(gòu)造函數(shù)為:
QHBoxLayout([QWidget parent]),[]表示參數(shù)為可選
QVBoxLayout:垂直布局蟆淀,在垂直方向上排列控件太援,即:上下排列。構(gòu)造函數(shù)為:
QVBoxLayout([QWidget parent]),[]表示參數(shù)為可選
這兩類的繼承層次結(jié)構(gòu)如下:
(QObject扳碍,QLayoutltem) - QLayout - QBoxLayout - QHBoxLayout
(QObject,QLayoutltem) - QLayout - QBoxLayout - QVBoxLayout
這兩個(gè)類都不是QWidget類的繼承者仙蛉,因此沒有自己的窗口笋敞,不能單獨(dú)使用。 因此荠瘪,容器作為子控件使用夯巷。
可以在QHBoxLayout和QVBoxLayout類的構(gòu)造函數(shù)中指定父組件,也可將容器作參數(shù)哀墓,調(diào)用父組件的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() # 創(chuàng)建容器
hbox.addWidget(button1) # 添加組件
hbox.addWidget(button2)
window.setLayout(hbox) # 指定父組件
window.show()
sys.exit(app.exec_())
相關(guān)的方法有:
addWidget( ) -添加組件到容器的末尾,其格式為:
addWidget (Component [, stretch = 0] [, alignment = 0])
參數(shù)1:要加入容器的組件
參數(shù)2:拉伸方式篮绰,可選項(xiàng)
參數(shù)3:對(duì)齊方式后雷,可選項(xiàng)
參數(shù)2,3可按順序或指定名稱設(shè)置參數(shù):
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,添加到容器的末尾贾漏。其他參數(shù)同addWidget( )候学。
hbox.addWidget (button1)
hbox.insertWidget (-1, button2) # 添加到容器的末尾
hbox.insertWidget (0, button3) # 添加到容器的最前面
removeWidget(Component) - 從容器中刪除組件;
replaceWidget( ) - 替換容器中的組件。格式為:
replaceWidget (component1, component2 [, options= FindChildrenRecursively])
參數(shù)1:被替換的組件
參數(shù)2:用來替換的組件
參數(shù)3:查找和替換方式纵散∈崧耄可以是QtCore.Qt中的FindDirectChildrenOnly(查找直接子組件)或FindChildrenRecursively( 遞歸查找子組件)。
addLayout( ) - 將另一個(gè)容器添加到當(dāng)前容器的末尾伍掀。 使用這種方法掰茶,可以將一個(gè)容器放入另一個(gè)容器中,從而創(chuàng)建一個(gè)復(fù)雜的結(jié)構(gòu)硕盹。 格式:
addLayout(Container [符匾,stretch = 0])
insertLayout() - 將另一個(gè)容器添加到當(dāng)前容器的指定位置。 如果第一個(gè)參數(shù)設(shè)置為負(fù)值瘩例,則將容器添加到末尾啊胶。 方法格式:
insertLayout(Index,Container [,stretch = 0])
insertSpacing (Index>, Size) - 添加空白間隔到特定位置垛贤。間隔的大小以像素為單位焰坪。如果第一個(gè)參數(shù)為負(fù)值,那么間隔將被添加到容器的末尾;
addStretch([stretch = 0]) - 將可拉伸空間添加到容器的末端聘惦。
insertStretch (Index [, stretch = 0]) - 作用與addStretch( )相似某饰,增加了指定位置的參數(shù)。 Index:-1,添加到容器的末尾.
addWidget( )和insertwidget( )方法中的alignment參數(shù)用來設(shè)置添加到容器中組件的對(duì)齊方式黔漂。 可以指定QtCore.Qt類的以下屬性:
AlignLeft, 1: 水平左對(duì)齊
AlignRight, 2: 水平右對(duì)齊
AlignHCenter, 4:水平居中
AlignJustify,8: 填滿空間
AlignTop诫尽,32:垂直上對(duì)齊
AlignBottom,64:垂直下對(duì)齊
AlignVCenter, 128:垂直居中
AlignBaseiine 炬守,256:垂直基線對(duì)齊;
AlignCenter- AlignVCenter|AlignHCenter-垂直牧嫉、水平均居中;
AlignAbsolute,16:如果通過setLayoutDirection()函數(shù)將對(duì)齊方式設(shè)置為 QtCore.Qt.RightToLeft(默認(rèn)為QtCore.Qt.LeftToRight)减途,AlignLeft(水平左對(duì)齊)實(shí)際表現(xiàn)為水平右對(duì)齊酣藻。要確保AlignLeft(水平左對(duì)齊)實(shí)際表現(xiàn)為水平左對(duì)齊鳍置,要和AlignAbsolute同時(shí)使用辽剧,即:AlignAbsolute|AlignLeft。
setDirection (direction) - 設(shè)置容器內(nèi)組件的排列方式税产,direction可為QBoxLayout類的枚舉變量:
? LeftToRight - 0 - 從左到右(水平容器的默認(rèn)方式)
? RightToLeft - 1 - 從右到左
? TopToBottom-2 - 從上到下(垂直容器的默認(rèn)方式)
? BottomToTop - 3 - 從下到上
setContentsMargins () - 設(shè)置容器四周的空白邊界砖第,格式為:
setContentsMargins (Left,Top,Right, Bottom)
hbox.setContentsMargins (2, 4, 2, 4)
m = QtCore.QMargins (4, 2, 4, 2)
hbox.setContentsMargins (m)