一、QObject-对象父类、子类设置和查找
1.基本知识
- 设置父对象:obj1.setParent(obj2)
- 获取父对象:print(obj1.parent())
- 获取孩子:print(obj2.children())
- 获取直接的子对象:print(obj2.findChild(QObject))
- 获取所有的子对象:print(obj2.findChildren(QObject))
2.代码
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("父子关系")
self.resize(600,500)
self.func_list() #一定要调用
def func_list(self):
self.func()
def func(self):
obj1=QObject()
print('obj1',obj1)
obj2=QObject()
print('obj2',obj2)
obj3=QObject()
print('obj3',obj3)
#设置父类
obj2.setParent(obj1)
obj3.setParent(obj2)
#输出obj2的父类和孩子
print(obj2.parent())
print(obj2.children())
print(obj1.findChild(QObject)) #获取直接的子对象
print(obj1.findChildren(QObject))#获取所有的子对象
if __name__=='__main__':
app=QApplication(sys.argv)
window=Window()
window.show()
sys.exit(app.exec_())
3.运行结果
- 弹窗

- 控制台
