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

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

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

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

I know the previous question related to this, but there was no β€œreal” answer there.

+7
source share
2 answers

First, the object you emit from needs a signal defined as an attribute of its class:

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

Note that there is one argument of the type object in the signal that should pass something. Then you should be able to emit a signal from the class using an argument of any data type:

 self.valueChanged.emit(anyObject) 
+17
source

I'm a beginner, and this is the first question I'm trying to answer, so I apologize if I misunderstood the question ...

The following code emits a signal that a custom Python object sends, and this slot uses this 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() 
+5
source

All Articles