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


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!
Create GUI Applications with Python & Qt5
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

What does @Slot() do? was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.