-
Notifications
You must be signed in to change notification settings - Fork 215
/
08-QObject对象删除.py
42 lines (30 loc) · 1012 Bytes
/
08-QObject对象删除.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import sys
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QObject对象删除")
self.resize(500, 500)
self.move(400, 250)
self.setup_ui()
def setup_ui(self):
self.QObject对象删除()
def QObject对象删除(self):
# 删除一个对象时,也会解除与父对象的关系
obj1 = QObject()
self.obj1 = obj1
obj2 = QObject()
obj3 = QObject()
obj3.setParent(obj2)
obj2.setParent(obj1)
obj1.destroyed.connect(lambda: print("obj1被释放了"))
obj2.destroyed.connect(lambda: print("obj2被释放了"))
obj3.destroyed.connect(lambda: print("obj3被释放了"))
obj2.deleteLater() # 在下一次消息循环才销毁
print(obj1.children())
print(obj2)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())