八月
24
QObject父子对象的操作案例
import sys
from PySide2 import QtWidgets as qtw
app = qtw.QApplication()
# 当控件没有父控件时,会被自动包装为窗口,增加标题等功能
win_root = qtw.QWidget(None)
win_root.resize(500, 500)
label0 = qtw.QLabel(win_root)
label0.setText('label 0')
label1 = qtw.QLabel(win_root)
label1.setText('label 1')
label1.move(0, 30)
label2 = qtw.QLabel(win_root)
label2.setText('label 2')
label2.move(0, 60)
label3 = qtw.QLabel(win_root)
label3.setText('label 3')
label3.move(0, 90)
btn = qtw.QPushButton(win_root)
btn.setText('button')
btn.move(0, 120)
# 给所有类型为QLabel的子控件设置背景色为cyan
for widget in win_root.findChildren(qtw.QLabel):
widget.setStyleSheet('background-color: cyan;')
win_root.show()
sys.exit(app.exec_())