Should I decorate slots in Pyside2 and if so how?

Heads up! You've already completed this tutorial.

RaSt | 2020-12-13 13:54:24 UTC | #1

Should I decorate slots in Pyside2 and if so how?

Example code of 2 different slots:

def createActions(self):

    buttonTriggered = QAction('Button Triggered', self)

    buttonToggled = QAction('Button Toggled', self)
    buttonToggled.toggled.connect(lambda checked: self.onButtonToggled(checked))

def onButtonTriggered(self):

def onButtonToggled(self, checked):


martin | 2020-12-18 00:34:01 UTC | #2

Hi RaSt welcome to the forum. You can decorate PySide2 slots the same way you do for PyQt5, just using the decorator

from PySide2.QtCore import Slot

def slot

...rather than...

from PyQt5.QtCore import pyqtSlot

def slot

...for PyQt5.

As to whether you should decorate the slots, it's a little trickier to answer -- but generally speaking no, you don't need to.

The only place I know the slot decorator is needed is when a) using threads, as it ensures the decorated method is started in the correct thread, or b) when you want to explicitly map a given slot to a specific call signature (types) in C++.

In your examples, the slots are running in the GUI thread and signal/slots are single-typed, so you don't need them.

RaSt | 2020-12-18 00:33:58 UTC | #3

hi martin, thanks for your answer. so I won't do it

1:1 Coaching & Tutoring for your Python GUIs project
Martin Fitzpatrick Python GUIs Coaching & Training
60 mins ($195) More info

1:1 Python GUIs Coaching & Training

Comprehensive code reviewBugfixes & improvements • Maintainability advice and architecture improvements • Design and usability assessment • Suggestions and tips to expand your knowledgePackaging and distribution help for Windows, Mac & Linux • Find out more.

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

Should I decorate slots in Pyside2 and if so how? 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.