Is the PySide Slot Decorator Necessary? Is the PySide Slot Decorator Necessary? python python

Is the PySide Slot Decorator Necessary?


This link explains the following about the pyqtSlot decorator:

Although PyQt4 allows any Python callable to be used as a slot when connecting signals, it is sometimes necessary to explicitly mark a Python method as being a Qt slot and to provide a C++ signature for it. PyQt4 provides the pyqtSlot() function decorator to do this.

and

Connecting a signal to a decorated Python method also has the advantage of reducing the amount of memory used and is slightly faster.

Since the pyqtSlot decorator can take additional argument such as name, it allows different Python methods to handle the different signatures of a signal.

If you don't use the slot decorator, the signal connection mechanism has to manually work out all the type conversions to map from the underlying C++ function signatures to the Python functions. When the slot decorators are used, the type mapping can be explicit.


Austin has a good answer, and the answer I'm about to write is a bit outside the scope of your question, but it's something that has been confusing me and I imagine others will end up on this page wondering the same thing.

If you want to expose Python methods to JavaScript (using QTWebKit), then the @pyqtSlot decorator is mandatory. Undecorated methods are not exposed to JavaScript.


In a multithreaded environment, it may be mandatory not to use the pyside Slot decorator, because it can cause signals to go to the wrong thread. See

Derived classes receiving signals in wrong thread in PySide (Qt/PyQt)

https://bugreports.qt.io/browse/PYSIDE-249