Is it mandatory to use the pyqtSlot decorator?

Asked

Viewed 159 times

2

When I want to connect a QPushButtonto an event, for example, I use a method and add it as event callback clicked.connect

For example:

def buildUi(self):

    self.buttonSubmitText  = QtWidgets.QPushButton("Enviar")

    self.textChat = QtWidgets.QLineEdit()

    self.buttonSubmitText.clicked.connect(self.onSubmitText)



 def onSubmitText(self):

    text = self.textChat.text()

    message = create_message(self.selectedUser["id"], text)

    formattedText = self._buildChatText(message)

    self.viewChat.insertHtml(formattedText)

    self.textChat.setText("")

But I’ve seen several examples on the internet where it’s added QtCore.pyqtSlot() as decorator. That is, the code looks like this:

 @QtCore.pyqtSlot()
 def onSubmitText(self):

    text = self.textChat.text()

    message = create_message(self.selectedUser["id"], text)

    formattedText = self._buildChatText(message)

    self.viewChat.insertHtml(formattedText)

    self.textChat.setText("")

I would like to know what is the impact of using or not using this decorator? It makes a difference?

It is mandatory to use it or not?

1 answer

2


As stated in Soen, this link explains about the decorated (translated):

Although Pyqt4 allows anything that can be called in Python (as methods) to be used as a slot when connecting signals, it is sometimes necessary to explicitly mark a Python method as a slot Qt and provide a subscription for him. Pyqt4 provides the decorator pyqtSlot() function to do this.

And this other passage (translated):

When connecting a signal with a decorator method has the advantage in reducing the memory consumption used, thus being a little faster.

If you don’t use the slots, signal connection mechanism has to manually calculate all type conversions to map function signatures for Python functions, when slots are used, type mapping can be explicit.

  • 1

    Very good. So I’ll use it on everyone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.