除了QHBoxLayout水平布局類和QVBoxLayout垂直布局類外,用作組件對齊的類還有QGridLayout網(wǎng)格布局類。他的繼承層次結(jié)構(gòu)如下:
(QObject,QLayoutltem) - QLayout - QGridLayout
構(gòu)造函數(shù)為:
QGridLayout([[QWidget parent])
與QHBoxLayout和QVBoxLayout一樣,不是QWidget類的繼承者,因此沒有自己的窗口,不能單獨使用。 因此,容器作為子控件使用。要在構(gòu)造函數(shù)中指定父組件。如果沒有,可將容器作參數(shù),調(diào)用父組件的setLayout( )方法。使用示例:
-- coding: utf-8 --
from PyQt5 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget() # 父組件
window.setWindowTitle("QGridLayout")
window.resize(150, 100)
button1 = QtWidgets.QPushButton("1")
button2 = QtWidgets.QPushButton("2")
button3 = QtWidgets.QPushButton("3")
button4 = QtWidgets.QPushButton("4")
grid = QtWidgets.QGridLayout() #創(chuàng)建網(wǎng)格
grid.addWidget(button1, 0, 0) # 添加組件
grid.addWidget(button2, 0, 1)
grid.addWidget(button3, 1, 0)
grid.addWidget(button4, 1, 1)
window.setLayout(grid) # 指定父組件
window.show()
sys.exit(app.exec_())
該類的方法有:
addwidget( ) -添加組件到網(wǎng)格。函數(shù)格式:
addWidget(Component,Row,Column [, alignment = 0])
addWidget(Component,Row,Column,NRows, NColumns [, alignment = 0])
函數(shù)1的4個參數(shù):
參數(shù)1:要加入容器的組件
參數(shù)2:組件所在行,0為第一行
參數(shù)3:組件所在列,0為第一列
參數(shù)4:對齊方式,可選項
函數(shù)2的4個參數(shù):
參數(shù)1:要加入容器的組件
參數(shù)2:組件所在行,0為第一行
參數(shù)3:組件所在列,0為第一列
參數(shù)4:組件占用的行數(shù)
參數(shù)5:組件占用的列數(shù)
參數(shù)6:對齊方式,可選項
addLayout( ) - 添中容器到網(wǎng)格中。格式為:
addLayout(Component,Row,Column [, alignment = 0])
addLayout(Component,Row,Column,NRows, NColumns [, alignment = 0])
參數(shù)1:要加入容器的容器。其他參數(shù)同addWidet( ).
要刪除和替換組件,使用removeWidget()和replaceWidget()方法,詳見PyQt5編程(24)。
此外,該類的方法還有setRowMinimumHeight( ),setColumnMinimumWidth( ),setRowStretch( ),setColumnStretch( )等,詳見http://doc.qt.io/qt-5/qgridlayout.html
<wbr> <wbr> 除了QHBoxLayout水平布局類和
QVBoxLayout垂直布局類外,用作組件對齊的類還有
<font face="Microsoft YaHei, Helvetica Neue, SimSun" color="#494949">QGridLayout網(wǎng)格布局類。他的繼承層次結(jié)構(gòu)如下:</font>
<font face="Microsoft YaHei, Helvetica Neue, SimSun" color="#494949"> <wbr> <wbr> <wbr>(QObject,QLayoutltem) - QLayout - QGridLayout
<wbr> <wbr> 構(gòu)造函數(shù)為:</font>
<font face="Microsoft YaHei, Helvetica Neue, SimSun" color="#494949"> <wbr> <wbr> QGridLayout([</font>
[QWidget <wbr>
<wbr style="color: rgb(73, 73, 73); font-family:" microsoft="" background-color:="">
p
<font face="Microsoft YaHei, Helvetica Neue, SimSun" color="#494949">arent])</font>
<font face="Microsoft YaHei, Helvetica Neue, SimSun" color="#494949"> <wbr> <wbr> 與</font>QHBoxLayout和QVBoxLayout一樣,不是QWidget類的繼承者,因此沒有自己的窗口,不能單獨使用。 因此,容器作為子控件使用。要在構(gòu)造函數(shù)中指定父組件。如果沒有,可將容器作參數(shù),調(diào)用父組件的setLayout( )方法。使用示例:
-- coding: utf-8 --
from PyQt5 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget() <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr># 父組件
window.setWindowTitle("QGridLayout")
window.resize(150, 100)
button1 = QtWidgets.QPushButton("1")
button2 = QtWidgets.QPushButton("2")
button3 = QtWidgets.QPushButton("3")
button4 = QtWidgets.QPushButton("4")
grid = QtWidgets.QGridLayout() <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>#創(chuàng)建網(wǎng)格
grid.addWidget(button1, 0, 0) <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> # 添加組件
grid.addWidget(button2, 0, 1)
grid.addWidget(button3, 1, 0)
grid.addWidget(button4, 1, 1)
window.setLayout(grid) <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>
<wbr>
<wbr style="color: rgb(73, 73, 73); font-family:" microsoft="" background-color:="">
指定父組件
window.show()
sys.exit(app.exec_())
<wbr> <wbr> 該類的方法有:
- addwidget( ) -添加組件到網(wǎng)格。函數(shù)格式:
addWidget(Component,Row,Column [, alignment = 0])
addWidget(Component,Row,Column,NRows, NColumns [, alignment = 0])
函數(shù)1的4個參數(shù):
參數(shù)1:要加入容器的組件
參數(shù)2:組件所在行,0為第一行
參數(shù)3:組件所在列,0為第一列
參數(shù)4:對齊方式,可選項
函數(shù)2的4個參數(shù):
參數(shù)1:要加入容器的組件
參數(shù)2:組件所在行,0為第一行
參數(shù)3:組件所在列,0為第一列
參數(shù)4:組件占用的行數(shù)
參數(shù)5:組件占用的列數(shù)
參數(shù)6:對齊方式,可選項 - addLayout( ) - 添中容器到網(wǎng)格中。格式為:
<wbr>addLayout(Component,Row,Column [, alignment = 0])
addLayout(Component,Row,Column,NRows, NColumns [, alignment = 0])
參數(shù)1:要加入容器的容器。其他參數(shù)同addWidet( ).
<wbr> <wbr> <wbr>要刪除和替換組件,使用removeWidget()和replaceWidget()方法,詳見PyQt5編程(24)。
<wbr> <wbr> 此外,該類的方法還有setRowMinimumHeight( ),setColumnMinimumWidth( ),setRowStretch( ),setColumnStretch( )等,詳見http://doc.qt.io/qt-5/qgridlayout.html