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:

python
def createActions(self):

    buttonTriggered = QAction('Button Triggered', self)
    buttonTriggered.triggered.connect(self.onButtonTriggered)

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

def onButtonTriggered(self):
    pass

def onButtonToggled(self, checked):
    pass

Thanks


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

python
from PySide2.QtCore import Slot

@Slot
def slot
    pass

...rather than...

python
from PyQt5.QtCore import pyqtSlot

@pyqtSlot
def slot
    pass

...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


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