forked from muziing/PyQt_practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-pyqtSignal-装饰器自动连接.py
59 lines (44 loc) · 1.37 KB
/
03-pyqtSignal-装饰器自动连接.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sys
from PyQt5.Qt import *
"""
1.
QtCore.QMetaObject.connectSlotsByName(obj)
将obj内部的子孙对象的信号,按照其objectNames连接到相关的槽函数
2.借助装饰器装饰固定规则的槽函数即可
代码:
@pyqtSlot()
def on_objectName_信号():
pass
等同于
obj.objectName.信号.connect(obj.objectName_信号)
def objectName_信号():
pass
"""
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("pyqtSignal装饰器连接")
self.resize(500, 500)
self.move(400, 250)
self.setup_ui()
def setup_ui(self):
btn = QPushButton("测试按钮", self)
self.btn = btn
btn.setObjectName("btn")
btn.resize(200, 200)
btn.move(100, 100)
QMetaObject.connectSlotsByName(self) # 非动态识别,必须先创建子孙对象,再调用这个方法才有效
@pyqtSlot(bool)
def on_btn_clicked(self, val):
print("按钮被点击了", val)
# ----------相当于下面的写法--------------
def do_something(self):
self.btn.clicked[bool].connect(self.btn_clicked)
def btn_clicked(self, val: bool):
print("按钮被点击了", val)
# ------------------------------------
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())