訊號與槽N對N

星空28發表於2024-06-08

···
from PyQt5.QtCore import *

class NNSignal(QObject):

# 定義一個訊號
signal1 = pyqtSignal()
# 傳送3個引數的訊號
signal2 = pyqtSignal(int)
signal3 = pyqtSignal()

def __init__(self):
    super().__init__()
    self.signal1.connect(self.signal_call1)
    self.signal1.connect(self.signal_call11)
    self.signal2.connect(self.signal1)
    self.signal3.connect(self.signal_call1)

    self.signal1.emit()
    self.signal2.emit(10)     # signal1關聯的是訊號1,所以觸發的還是訊號1
    self.signal3.emit()

    # 解除關聯
    self.signal1.disconnect(self.signal_call1)
    self.signal1.disconnect(self.signal_call11)
    self.signal2.disconnect(self.signal1)

    self.signal1.connect(self.signal_call1)
    self.signal2.connect(self.signal_call2)

    self.signal1.emit()
    self.signal2.emit(100)

def signal_call1(self):
    print("signal1 emit")

def signal_call11(self):
    print("signal1 call 11")

def signal_call2(self, val):
    print("signal2 emit, value=", val)

if name == 'main':
NNSignal()

相關文章