PyQt signal with arguments of arbitrary type / PyQt_PyObject equivalent for new-style signals

[亡魂溺海] 提交于 2020-01-20 05:50:08

问题


I have an object that should signal that a value has changed by emitting a signal with the new value as an argument. The type of the value can change, and so I'm unsure of how to write the signal type. I know that I can acconmplish this using old-style signals like this:

self.emit(SIGNAL("valueChanged(PyQt_PyObject)"), newvalue)

but how would I write this using new-style signals?

I am aware of a previous question related to this but no "real" answer was given there.


回答1:


First, the object you're emitting from needs the signal defined as an attribute of its class:

class SomeClass(QObject):
    valueChanged = pyqtSignal(object)  

Notice the signal has one argument of type object, which should allow anything to pass through. Then, you should be able to emit the signal from within the class using an argument of any data type:

self.valueChanged.emit(anyObject)



回答2:


I'm a beginner and this is the first question I'm attempting to answer, so apologies if I have misunderstood the question...

The following code emits a signal that sends a custom Python object, and the slot uses that class to print "Hello World".

import sys
from PyQt4.QtCore import pyqtSignal, QObject

class NativePythonObject(object):
    def __init__(self, message):
        self.message = message

    def printMessage(self):
        print(self.message)
        sys.exit()

class SignalEmitter(QObject):
    theSignal = pyqtSignal(NativePythonObject)

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

    def emitSignal(self):
        self.theSignal.emit(toBeSent)

class ClassWithSlot(object):
    def __init__(self, signalEmitter):
        self.signalEmitter = signalEmitter
        self.signalEmitter.theSignal.connect(self.theSlot)

    def theSlot(self, ourNativePythonType):
        ourNativePythonType.printMessage()

if __name__ == "__main__":
    toBeSent = NativePythonObject("Hello World")
    signalEmitter = SignalEmitter(toBeSent)
    classWithSlot = ClassWithSlot(signalEmitter)
    signalEmitter.emitSignal()


来源:https://stackoverflow.com/questions/4523006/pyqt-signal-with-arguments-of-arbitrary-type-pyqt-pyobject-equivalent-for-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!