Why do I need to decorate connected slots with pyqtSlot?
Question:
I’m using pyqt5, and I have several methods connected using code similar to the following:
self.progress.canceled.connect(self.cancel)
Where, for example, self.cancel
is:
def cancel(self):
self.timer.stop()
This code seems to work cleanly in multiple scenarios, without ever decorating cancel
with pyqtSlot
or doing anything special with it.
My questions are:
- What am I losing by doing it this way?
- What is the reason
pyqtSlot
is required?
Answers:
The main purpose of pyqtSlot
is to allow several different overloads of a slot to be defined, each with a different signature. It may also be needed sometimes when making cross-thread connections (see this answer for one such scenario). However, these use-cases are relatively rare, and in most PyQt applications it is not necessary to use pyqtSlot
at all. Signals can be connected to any python callable object, whether it is decorated as a slot or not.
The PyQt docs also state:
Connecting a signal to a decorated Python method also has the advantage of reducing the amount of memory used and is slightly faster.
However, in practice, this advantage is generally very small and will often be swamped by other factors. It has very little affect on raw signalling speed, and it would be necessary to make thousands of emits/connections before it started to have a significant impact on cpu load or memory usage. For an in depth analysis of these points, see this Code Project article by @Schollii:
Another advantage is when you design the custom widgets for Qt-designer, methods decorated by pyqtSlot
could be selected within Signal/Slot Editor
, otherwise you need to write the code.
I’m using pyqt5, and I have several methods connected using code similar to the following:
self.progress.canceled.connect(self.cancel)
Where, for example, self.cancel
is:
def cancel(self):
self.timer.stop()
This code seems to work cleanly in multiple scenarios, without ever decorating cancel
with pyqtSlot
or doing anything special with it.
My questions are:
- What am I losing by doing it this way?
- What is the reason
pyqtSlot
is required?
The main purpose of pyqtSlot
is to allow several different overloads of a slot to be defined, each with a different signature. It may also be needed sometimes when making cross-thread connections (see this answer for one such scenario). However, these use-cases are relatively rare, and in most PyQt applications it is not necessary to use pyqtSlot
at all. Signals can be connected to any python callable object, whether it is decorated as a slot or not.
The PyQt docs also state:
Connecting a signal to a decorated Python method also has the advantage of reducing the amount of memory used and is slightly faster.
However, in practice, this advantage is generally very small and will often be swamped by other factors. It has very little affect on raw signalling speed, and it would be necessary to make thousands of emits/connections before it started to have a significant impact on cpu load or memory usage. For an in depth analysis of these points, see this Code Project article by @Schollii:
Another advantage is when you design the custom widgets for Qt-designer, methods decorated by pyqtSlot
could be selected within Signal/Slot Editor
, otherwise you need to write the code.