Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyqt signal emit with object instance or None

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?

like image 893
Sebastian Elsner Avatar asked Feb 19 '23 10:02

Sebastian Elsner


1 Answers

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
like image 88
Maspe36 Avatar answered Feb 22 '23 05:02

Maspe36