Pyqt Signal With Arguments Of Arbitrary Type / Pyqt_pyobject Equivalent For New-style Signals
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 w
Solution 1:
First, the object you're emitting from needs the signal defined as an attribute of its class:
classSomeClass(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)
Solution 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
classNativePythonObject(object):
def__init__(self, message):
self.message = message
defprintMessage(self):
print(self.message)
sys.exit()
classSignalEmitter(QObject):
theSignal = pyqtSignal(NativePythonObject)
def__init__(self, toBeSent, parent=None):
super(SignalEmitter, self).__init__(parent)
self.toBeSent = toBeSent
defemitSignal(self):
self.theSignal.emit(toBeSent)
classClassWithSlot(object):
def__init__(self, signalEmitter):
self.signalEmitter = signalEmitter
self.signalEmitter.theSignal.connect(self.theSlot)
deftheSlot(self, ourNativePythonType):
ourNativePythonType.printMessage()
if __name__ == "__main__":
toBeSent = NativePythonObject("Hello World")
signalEmitter = SignalEmitter(toBeSent)
classWithSlot = ClassWithSlot(signalEmitter)
signalEmitter.emitSignal()
Post a Comment for "Pyqt Signal With Arguments Of Arbitrary Type / Pyqt_pyobject Equivalent For New-style Signals"