PyQT5之多個訊號

星空28發表於2024-06-08

···
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()

···

相關文章