What does @Slot() do?

Heads up! You've already completed this tutorial.

Anonymous submitter

Excellent tutorial! I appreciate your effort to write for both PyQT5 and PySide2 libraries. Can we get some more information on the @Slot() lines in the following tutorial? The code works without as well (PySide2 QT5.2.13 on *ubuntu1910).

Multithreading PyQt applications with QThreadPool


PedanticHacker

On the PyQt5 site, it is stated that

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

But then this site challenges that statementand provides some performance & memory tests.


Martin Fitzpatrick

I actually had the same thought when working on the recent updates to the book -- is this still necessary?! -- and tested removing it from the examples. It worked fine in some cases, but in others would give to segfaults. Reading up on it I understand the distinction as --

  1. if you decorate a method with @pyqtSlot then that slot is created as a native Qt slot, and behaves identically
  2. if you don't decorate the method then PyQt with create a "proxy" object wrapper which provides a native slot to Qt

The second raises an issue in threading re: where the proxy object should live -- in the GUI thread or the executing thread? In the case of the run method for runners it should always be in the runner thread, but it can (apparently?) potentially end up in the wrong place, leaving vars to be accessed from two separate threads (= a segfault). That said, this mechanism all seems to be particular to PyQt5.

I should do some more in depth testing, see if I can get a reproducible example.

Over 10,000 developers have bought Create GUI Applications with Python & Qt!

To support developers in [[ countryRegion ]] I give a [[ localizedDiscount[couponCode] ]]% discount with the code [[ couponCode ]] — Enjoy!

For [[ activeDiscount.description ]] I'm giving a [[ activeDiscount.discount ]]% discount with the code [[ couponCode ]] — Enjoy!

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak