Is it mandatory to use the decorator pyqtSlot?

2

When I want to connect a QPushButton to an event, for example, I use a method and add it as callback of event 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("")

However I have seen several examples on the internet where QtCore.pyqtSlot() is added as a 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 wonder what is the impact of using or not using this decorator? Does it make any difference?

Is it mandatory to use it or not?

    
asked by anonymous 24.03.2017 / 13:12

1 answer

2

As this answer in the Soen , this link explains about the set (translated):

  

Although PyQt4 allow 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 signature for it. PyQt4 provides the pyqtSlot() decorator function to do this.

And this other stretch (translated):

  

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

If you do not use the decorator slots , the signal connection mechanism has to manually calculate all type conversions to map the function signatures for Python functions, since when the decorators slots are used, the mapping types can be explicit.

    
24.03.2017 / 17:21