PyQT5之多个信号
···
from PyQt5.QtCore import *
class MultiSignal(QObject):
# 定义一个信号
signal1 = pyqtSignal()
# 发送3个参数的信号
signal2 = pyqtSignal(int)
signal3 = pyqtSignal(int, str)
signal4 = pyqtSignal(list)
signal5 = pyqtSignal(dict)
# 声明一个重载版本的信号,也就是槽函数的参数可以是int和str类型,也可以只有一个str类型的参数
signal6 = pyqtSignal([int, str], [str])
def __init__(self):
super().__init__()
self.signal1.connect(self.signal_call1)
self.signal2.connect(self.signal_call2)
self.signal3.connect(self.signal_call3)
self.signal4.connect(self.signal_call4)
self.signal5.connect(self.signal_call5)
self.signal6[int, str].connect(self.signal_call6) # 默认关联到第一个上
self.signal6[str].connect(self.signal_call6_overload) # 关联到第二个上
self.signal1.emit()
self.signal2.emit(10)
self.signal3.emit(1, "hello world")
self.signal4.emit([1, 2, 3])
self.signal5.emit({"name": "Jack", "age": 30})
self.signal6[str].emit("test6")
self.signal6[int, str].emit(20, "test")
def signal_call1(self):
print("signal1 emit")
def signal_call2(self, val):
print("signal2 emit, value=", val)
def signal_call3(self, val, text):
print("signal3 emit, value=", val, text)
def signal_call4(self, val):
print("signal4 emit, value=", val)
def signal_call5(self, val):
print("signal5 emit, value=", val)
def signal_call6(self, val, text):
print("signal6 emit, value=", val, text)
def signal_call6_overload(self, val):
print("signal6 overload emit, value=", val)
class MySlot(QObject):
def get(self, msg):
print("信息:" + msg)
def get1(self, msg, a, b):
print("信息:" + msg, "今天是{}月{}日".format(a, b))
if name == 'main':
MultiSignal()
···