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.

Create GUI Applications with Python & Qt5 by Martin Fitzpatrick — (PyQt5 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book


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.

Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

More info Get the book

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.