其他分享
首页 > 其他分享> > PyQtDay04--对象的父子关系+信号的操作

PyQtDay04--对象的父子关系+信号的操作

作者:互联网

1.对象的父子关系

(1)API及内存管理机制

实际操作:

 1 from PyQt5.Qt import *
 2 
 3 class Window(QWidget):
 4     def __init__(self):
 5         super().__init__()
 6         self.setWindowTitle("QObject的学习")
 7         self.resize(500, 500)
 8         self.setup_ui()
 9 
10     def setup_ui(self):
11         #self.QObject_ceshi()
12         self.QObject_对象的父子关系操作()
13 
14     def QObject_对象的父子关系操作(self):
15         # **********测试API************开始
16         # obj1=QObject()
17         # obj2=QObject()
18         # obj3=QObject()
19         #
20         # print('obj1',obj1)  # 0x0000028ED85CF1F8
21         # print('obj2', obj2) # 0x0000028ED85CF0D8
22         # print('obj3',obj3)  # 0x0000013E9DE6F168
23         #
24         # obj1.setParent(obj2)
25         # obj3.setParent(obj2)
26         # print(obj1.parent()) # 0x0000028ED85CF0D8
27         # print(obj2.children()) # 两个
28         # **********测试API************结束
29 
30         # ********内存管理机制**********开始
31         obj1=QObject()
32         obj2=QObject()
33 
34         obj2.setParent(obj1)
35 
36         # 开始监听obj2对象被释放
37         obj2.destroyed.connect(lambda:print('obj2对象被释放了')) # obj2对象被释放了
38 
39         # *********内存管理机制*********结束
40 
41 
42 if __name__ == '__main__':
43     import sys
44 
45     app=QApplication(sys.argv)
46     window=Window()
47     window.show()
48     sys.exit(app.exec_())

(2)应用场景

(3)【案例】

 1 from PyQt5.Qt import *
 2 
 3 class Window(QWidget):
 4     def __init__(self):
 5         super().__init__()
 6         self.setWindowTitle("QObject的学习")
 7         self.resize(500, 500)
 8         self.setup_ui()
 9 
10     def setup_ui(self):
11         #self.QObject_ceshi()
12         self.QObject_对象的父子关系操作()
13 
14     def QObject_对象的父子关系操作(self):
15         # **********测试API************开始
16         # obj1=QObject()
17         # obj2=QObject()
18         # obj3=QObject()
19         #
20         # print('obj1',obj1)  # 0x0000028ED85CF1F8
21         # print('obj2', obj2) # 0x0000028ED85CF0D8
22         # print('obj3',obj3)  # 0x0000013E9DE6F168
23         #
24         # obj1.setParent(obj2)
25         # obj3.setParent(obj2)
26         # print(obj1.parent()) # 0x0000028ED85CF0D8
27         # print(obj2.children()) # 两个
28         # **********测试API************结束
29 
30         # ********内存管理机制**********开始
31         obj1=QObject()
32         obj2=QObject()
33 
34         obj2.setParent(obj1)
35 
36         # 开始监听obj2对象被释放
37         obj2.destroyed.connect(lambda:print('obj2对象被释放了')) # obj2对象被释放了
38 
39         # *********内存管理机制*********结束
40 
41 
42 if __name__ == '__main__':
43     import sys
44 
45     app=QApplication(sys.argv)
46     # window=Window()
47     # window.show()
48 
49     # win1=QWidget()
50     # win1.setWindowTitle("红色")
51     # win1.setStyleSheet("background-color:red")
52     # win1.show()
53     #
54     # win2=QWidget()
55     # win2.setWindowTitle("绿色")
56     # win2.setStyleSheet("background-color:green")
57     # # win2.setParent(win1)
58     # win2.resize(200,200)
59     # win2.move(220,120)
60     # win2.show()
61 
62     win_root=QWidget()
63 
64     label1=QLabel()
65     label1.setText('label1')
66     label1.setParent(win_root)
67     label2 = QLabel()
68     label2.move(50,50)
69     label2.setText('label2')
70     label2.setParent(win_root)
71 
72     btn=QPushButton(win_root)
73     btn.move(100,100)
74     btn.setText('btn')
75 
76     win_root.show()
77 
78     for sub_widget in win_root.findChildren(QLabel):
79         print(sub_widget)
80         sub_widget.setStyleSheet("background-color:green")
81 
82     sys.exit(app.exec_())
View Code

 

2、信号的操作

(1)API操作

 1 from PyQt5.Qt import *
 2 
 3 class Window(QWidget):
 4     def __init__(self):
 5         super().__init__()
 6         self.setWindowTitle("QObject的学习")
 7         self.resize(500, 500)
 8         self.setup_ui()
 9 
10     def setup_ui(self):
11         self.QObject_信号的操作()
12 
13     def QObject_信号的操作(self):
14         self.obj=QObject()
15         **********测试API************开始
16         obj=QObject()
17         # obj.destroyed
18         # obj.objNameChanged
19         def destroy_cao():
20             print("对象被释放了")
21         obj.destroyed.connect(destroy_cao)
22 
23         def obj_name_cao(name):
24             print("对象名称发生了改变",name)
25         self.obj.objectNameChanged.connect(obj_name_cao)
26         self.obj.setObjectName('XXX')
27         print(self.obj.signalsBlocked(),'1')
28 
29         self.obj.blockSignals(True)  # 临时中断连接
30         # self.obj.objectNameChanged.disconnect()
31         self.obj.setObjectName('ooo')
32         print(self.obj.signalsBlocked(), '2')
33 
34         self.obj.blockSignals(False)  # 恢复连接
35         self.obj.setObjectName('hhhh')
36         print(self.obj.signalsBlocked(), '3')
37         **********测试API************结束
38 
39         ***********信号与槽按钮****************开始
40         btn=QPushButton(self)
41         btn.setText('点击我')
42 
43         def cao():
44             print("点我做啥?")
45 
46         btn.clicked.connect(cao)
47 
48         ***********信号与槽按钮****************结束
49         
50 
51 if __name__ == '__main__':
52     import sys
53     window=Window()
54     window.show()
55     sys.exit(app.exec_())

(2)应用场景

(3)案例

 1 from PyQt5.Qt import *
 2 
 3 if __name__ == '__main__':
 4     import sys
 5 
 6     app=QApplication(sys.argv)
 7     window=QWidget()
 8     # 连接窗口标题变化的信号 与 槽
 9     def cao(title):
10         print('窗口标题变化了',title)
11         # window.windowTitleChanged.disconnect()
12         window.blockSignals(True)
13         window.setWindowTitle('撩课-'+title)
14         window.blockSignals(False)
15         #window.windowTitleChanged.connect(cao)
16 
17     window.windowTitleChanged.connect(cao)
18     window.setWindowTitle("Hello Sz")
19     window.setWindowTitle("Hello Sz2")
20 
21     window.show()
22     sys.exit(app.exec_())
View Code

时间:2020-03-02       20:55:52

作者:931935931(QQ)

 

标签:__,obj2,obj,self,QObject,父子关系,信号,print,PyQtDay04
来源: https://www.cnblogs.com/fengxb1213/p/12397967.html