QBoxLayout
流程
label = QLabel('标签')
label_1 = QLabel('标签1')
label_2 = QLabel('标签2')
label_3 = QLabel('标签3')
label_4 = QLabel('标签4')
label_5 = QLabel('标签5')
label_6 = QLabel('标签6')
label.setStyleSheet('background-color:red;')
label_1.setStyleSheet('background-color:green;')
label_2.setStyleSheet('background-color:blue;')
label_3.setStyleSheet('background-color:yellow;')
label_4.setStyleSheet('background-color:pink;')
label_5.setStyleSheet('background-color:orange;')
label_6.setStyleSheet('background-color:cyan;')
layout = QBoxLayout(QBoxLayout.TopToBottom)
self.setLayout(layout)
layout.setSpacing(50)
layout.setContentsMargins(11, 20, 30, 40)
print(layout.contentsMargins().left())
layout.replaceWidget(label, label_3)
label.hide()
label.setParent(None)
label.destroyed.connect(lambda: print('label_1被销毁!'))
h_layout = QBoxLayout(QBoxLayout.LeftToRight)
h_layout.addWidget(label_4)
h_layout.addWidget(label_5)
h_layout.addWidget(label_6)
layout.addWidget(label_3)
layout.addWidget(label_1)
layout.addLayout(h_layout)
layout.addWidget(label_2)
layout.setEnabled(True)
布局方向
label = QLabel('标签')
label_1 = QLabel('标签1')
label_2 = QLabel('标签2')
label.setStyleSheet('background-color:red;')
label_1.setStyleSheet('background-color:green;')
label_2.setStyleSheet('background-color:blue;')
layout = QBoxLayout(QBoxLayout.TopToBottom)
layout.addWidget(label)
layout.addWidget(label_1)
layout.addWidget(label_2)
self.setLayout(layout)
timer = QTimer(self)
def func():
layout.setDirection((layout.direction() + 1) % 4)
timer.timeout.connect(func)
timer.start(1000)
添加元素
label = QLabel('标签')
label_1 = QLabel('标签1')
label_2 = QLabel('标签2')
label_3 = QLabel('标签3')
label.setStyleSheet('background-color:red;')
label_1.setStyleSheet('background-color:green;')
label_2.setStyleSheet('background-color:blue;')
label_3.setStyleSheet('background-color:yellow;')
layout = QBoxLayout(QBoxLayout.TopToBottom)
layout.addWidget(label)
layout.addSpacing(80)
layout.addWidget(label_1)
layout.addWidget(label_2)
self.setLayout(layout)
layout.insertWidget(1, label_3)
layout.removeWidget(label_3)
label_3.setParent(None)
layout.insertSpacing(4, 80)
伸缩因子
label = QLabel('标签')
label_1 = QLabel('标签1')
label_2 = QLabel('标签2')
label_3 = QLabel('标签3')
label.setStyleSheet('background-color:red;')
label_1.setStyleSheet('background-color:green;')
label_2.setStyleSheet('background-color:blue;')
label_3.setStyleSheet('background-color:yellow;')
layout = QBoxLayout(QBoxLayout.TopToBottom)
layout.addWidget(label)
layout.addStretch()
layout.addWidget(label_1)
layout.addStretch()
layout.addWidget(label_2)
self.setLayout(layout)
layout.setStretchFactor(label_2, 1)
水平布局
label = QLabel('标签')
label_1 = QLabel('标签1')
label_2 = QLabel('标签2')
label.setStyleSheet('background-color:red;')
label_1.setStyleSheet('background-color:green;')
label_2.setStyleSheet('background-color:blue;')
h_layout = QHBoxLayout()
h_layout.addWidget(label)
h_layout.addWidget(label_1)
h_layout.addWidget(label_2)
self.setLayout(h_layout)
h_layout.setDirection(QBoxLayout.RightToLeft)
QFormLayout
流程
label_name = QLabel('姓名:')
label_sex = QLabel('性别')
le_name = QLineEdit()
sb_age = QSpinBox()
m_rb = QRadioButton('男')
f_rb = QRadioButton('女')
lyt_sex = QHBoxLayout()
lyt_sex.addWidget(m_rb)
lyt_sex.addWidget(f_rb)
submit_btn = QPushButton('提交')
fl = QFormLayout()
self.setLayout(fl)
fl.addRow(label_name,le_name)
fl.addRow('年龄(&a):',sb_age)
fl.addRow(label_sex,lyt_sex)
fl.insertRow(-1,submit_btn)
print(fl.rowCount())
print(fl.getWidgetPosition(label_sex))
print(fl.getLayoutPosition(fl))
插入移除行
label_name = QLabel('姓名:')
label_age = QLabel('年龄:')
label_sex = QLabel('性别')
le_name = QLineEdit()
sb_age = QSpinBox()
m_rb = QRadioButton('男')
f_rb = QRadioButton('女')
lyt_sex = QHBoxLayout()
lyt_sex.addWidget(m_rb)
lyt_sex.addWidget(f_rb)
submit_btn = QPushButton('提交')
fl = QFormLayout()
self.setLayout(fl)
fl.setWidget(0, QFormLayout.LabelRole, label_name)
fl.addRow(label_age)
fl.setWidget(1, QFormLayout.FieldRole, label_sex)
fl.setWidget(2, QFormLayout.SpanningRole, submit_btn)
fl.removeRow(2)
fl.removeWidget(label_name)
''''#移除行(不删除子控件)
print(fl.takeRow(0).labelItem.widget())
print(fl.takeRow(0).fieldItem)
#必须彻底删除子控件
label_name.setParent(None)
label_age.setParent(None)'''
标签与行策略
label_name = QLabel('===' * 30)
le_name = QLineEdit()
fl = QFormLayout()
self.setLayout(fl)
fl.addRow(label_name, le_name)
fl.setRowWrapPolicy(QFormLayout.WrapAllRows)
对齐方式和间距
label_name = QLabel('==' * 10)
label_age = QLabel('年龄:')
le_name = QLineEdit()
sb_age = QSpinBox()
fl = QFormLayout()
self.setLayout(fl)
fl.addRow(label_name, le_name)
fl.addRow(label_age, sb_age)
fl.setFormAlignment(Qt.AlignLeft | Qt.AlignVCenter)
print(fl.formAlignment())
fl.setLabelAlignment(Qt.AlignCenter)
fl.setVerticalSpacing(30)
fl.setHorizontalSpacing(30)
QGridLayout
流程
self.label = QLabel('标签')
self.label_1 = QLabel('标签1')
self.label_2 = QLabel('标签2')
self.label.setStyleSheet('background-color:red;')
self.label_1.setStyleSheet('background-color:green;')
self.label_2.setStyleSheet('background-color:blue;')
gl = QGridLayout()
self.setLayout(gl)
gl.addWidget(self.label)
gl.addWidget(self.label_1, 0, 1)
gl.addWidget(self.label_2, 1, 0, 1, 2)
print(gl.getItemPosition(2))
最小列宽行高与拉伸系数
self.label = QLabel('标签')
self.label_1 = QLabel('标签1')
self.label_2 = QLabel('标签2')
self.label.setStyleSheet('background-color:red;')
self.label_1.setStyleSheet('background-color:green;')
self.label_2.setStyleSheet('background-color:blue;')
gl = QGridLayout()
self.setLayout(gl)
gl.addWidget(self.label)
gl.addWidget(self.label_1, 0, 1)
gl.addWidget(self.label_2, 1, 0, 1, 2)
gl.setColumnMinimumWidth(0, 100)
gl.setRowMinimumHeight(0, 100)
gl.setColumnStretch(0, 2)
gl.setColumnStretch(1, 1)
gl.setRowStretch(1, 1)
行列数与间距
self.label = QLabel('标签')
self.label_1 = QLabel('标签1')
self.label_2 = QLabel('标签2')
self.label.setStyleSheet('background-color:red;')
self.label_1.setStyleSheet('background-color:green;')
self.label_2.setStyleSheet('background-color:blue;')
gl = QGridLayout()
self.setLayout(gl)
gl.addWidget(self.label)
gl.addWidget(self.label_1, 0, 1)
gl.addWidget(self.label_2, 1, 0, 1, 2)
print(gl.spacing())
print(gl.horizontalSpacing())
print(gl.verticalSpacing())
gl.setVerticalSpacing(20)
gl.setHorizontalSpacing(20)
gl.setSpacing(20)
print(gl.rowCount())
print(gl.columnCount())
print(gl.cellRect(0, 1))
QStackedLayout
流程
sl = QStackedLayout()
self.setLayout(sl)
label = QLabel('标签')
label_1 = QLabel('标签1')
label_2 = QLabel('标签2')
label_3 = QLabel('标签3')
label_4 = QLabel('标签4')
label_5 = QLabel('标签5')
label_6 = QLabel('标签6')
label.setStyleSheet('background-color:red;')
label_1.setStyleSheet('background-color:green;')
label_2.setStyleSheet('background-color:blue;')
label_3.setStyleSheet('background-color:yellow;')
label_4.setStyleSheet('background-color:pink;')
label_5.setStyleSheet('background-color:orange;')
label_6.setStyleSheet('background-color:cyan;')
v_layout = QVBoxLayout()
v_layout.addWidget(label_4)
v_layout.addWidget(label_5)
v_layout.addWidget(label_6)
sl.addWidget(label)
sl.addWidget(label_1)
sl.addWidget(label_2)
简单方法
sl.setCurrentIndex(1)
sl.setCurrentWidget(label1)
print(sl.currentIndex())
sl.insertWidget(1, label_3)
print(sl.widget(2).text())
展示模式
sl = QStackedLayout()
self.setLayout(sl)
label = QLabel('标签')
label_1 = QLabel('标签1')
label_2 = QLabel('标签2')
label.setStyleSheet('background-color:red;')
label_1.setStyleSheet('background-color:green;')
label_2.setStyleSheet('background-color:blue;')
sl.addWidget(label)
sl.addWidget(label_1)
sl.addWidget(label_2)
sl.setStackingMode(QStackedLayout.StackAll)
label.setFixedSize(100, 100)
label_1.setFixedSize(200, 200)
切换和信号
sl = QStackedLayout()
self.setLayout(sl)
label = QLabel('标签')
label_1 = QLabel('标签1')
label_2 = QLabel('标签2')
label.setStyleSheet('background-color:red;')
label_1.setStyleSheet('background-color:green;')
label_2.setStyleSheet('background-color:blue;')
sl.addWidget(label)
sl.addWidget(label_1)
sl.addWidget(label_2)
''''#切换界面展示
sl.setCurrentIndex(1)
sl.setCurrentWidget(label_1)'''
timer = QTimer(self)
timer.timeout.connect(lambda: sl.setCurrentIndex((sl.currentIndex() + 1) % sl.count()))
timer.start(1000)
sl.currentChanged.connect(lambda calue: print(calue))
尺寸策略
建议最小尺寸
class Label(QLabel):
def minimumSizeHint(self):
'''最小的建议尺寸'''
return QSize(150, 150)
label = QLabel('标签')
label_1 = QLabel('标签1')
label_2 = Label('标签2')
label.setStyleSheet('background-color:red;')
label_1.setStyleSheet('background-color:green;')
label_2.setStyleSheet('background-color:blue;')
layout = QVBoxLayout()
self.setLayout(layout)
layout.addWidget(label)
layout.addWidget(label_1)
layout.addWidget(label_2)
建议尺寸
class MyLabel(QLabel):
def sizeHint(self):
'''建议尺寸'''
return QSize(150, 150)
label = MyLabel('标签')
label_1 = QLabel('标签1')
label_2 = QLabel('标签2')
label.setStyleSheet('background-color:red;')
label_1.setStyleSheet('background-color:green;')
label_2.setStyleSheet('background-color:blue;')
layout = QVBoxLayout()
self.setLayout(layout)
layout.addWidget(label)
layout.addWidget(label_1)
layout.addWidget(label_2)
缩放策略
"""Preferred #可伸展也可收缩(默认)
Expanding #也可伸缩,但是比Prefered优先级更高(当有两个及以上空间的时候油下锅)
Fixed #固定不变
Minimum #将建议尺寸作为最小尺寸,可以缩放但不能低于最小
Maximum #将建议尺寸作为最大尺寸,可以缩放但不能高于最大
Ignored #忽略SizeHint的作用"""
label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Preferred)
sp = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
sp.setRetainSizeWhenHidden(True)
label_1.setSizePolicy(sp)
label_1.hide()
label.setFixedSize(300, 100)