信号与槽N对N

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

posted @ 2024-06-08 09:56  星空28  阅读(3)  评论(0编辑  收藏  举报