重写子类中的PyQt信号

2024-10-03 23:20:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将泛型数据类的子类化,以便对父级计算的数据执行一些附加操作。我想在子类中重用dataReady信号。问题是信号仍然从父节点发出,并在附加计算完成之前触发连接的插槽。在

是否可以重写/抑制父类发出的信号,或者只需选择一个不同的信号名?在

下面是我的课程的一个简化示例:

class Generic(QtCore.QObject):
    dataReady = pyqtSignal()

    def __init__(self, parent=None):
        super(Generic, self).__init__(parent)

    def initData(self):
        # Perform computations
        ...

        self.dataReady.emit()

class MoreSpecific(Generic):
    dataReady = pyqtSignal()

    def __init__(self, parent=None):
        super(MoreSpecific, self).__init__(parent)  

    def initData(self):
        super(MoreSpecific, self).initData()

        # Further computations
        ...

        self.dataReady.emit()

Tags: 数据selfnone信号initdefgenericclass
2条回答

我会重新组织一下课程。在

class Generic(QtCore.QObject):
    dataReady = pyqtSignal()

    def __init__(self, parent=None):
        super(Generic, self).__init__(parent)

    def initData(self):
        self.computations()
        self.dataReady.emit()

    def computations(self):
        # put your computations in a method
        ...

class MoreSpecific(Generic):

    def __init__(self, parent=None):
        super(MoreSpecific, self).__init__(parent)  

    def computations(self):
        super(MoreSpecific, self).computations()
        # further computations

现在,您的initData方法不需要更改,它本应进行一些计算,然后发送信号,MoreSpecific类只发送一次信号。在

您可以使用blockSignals

def initData(self):
    self.blockSignals(True)
    super(MoreSpecific, self).initData()
    self.blockSignals(False)

相关问题 更多 >