I would like to have a pyqt signal, which can either emit with a python "object" subclass argument or None. For example, I can do this:
valueChanged = pyqtSignal([MyClass], ['QString'])
but not this:
valueChanged = pyqtSignal([MyClass], ['None'])
TypeError: C++ type 'None' is not supported as a pyqtSignal() type argument type
or this:
valueChanged = pyqtSignal([MyClass], [])
TypeError: signal valueChanged[MyObject] has 1 argument(s) but 0 provided
I also tried None without the quotes and the c++ equivalent "NULL". But neither seems to work. What is it I need to do to make this work?
A hacky workaround for this is to define your signal to emit object
. Since None
and MyClass
both inherit from object
it works as expected. This requires no casting in the slot.
Working in PyQt5 5.11.3 and Python 3.5.6
Signal
valueChanged = QtCore.pyqtSignal(object)
valueChanged.emit(MyClass())
valueChanged.emit(None)
Slot
valueChanged.connect(slot)
def slot(my_class_instance):
if my_class_instance:
my_class_instance.some_method()
else:
# my_class_instance is None
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With