function of pyqtSlot [duplicate]
Asked Answered
M

1

17

I just read a tutorial about pyqt5 button from here. And the code is as below. There is a question about button.clicked.connect(self.on_click) and @pyqtSlot(). If I delete @pyqtSlot() from the code, it still works. However, the button does not work if I delete button.clicked.connect(self.on_click) from the code. So what is the function of @pyqtSlot() in this code?

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 button - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 320
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('PyQt5 button', self)
        button.setToolTip('This is an example button')
        button.move(100,70) 
        button.clicked.connect(self.on_click)

        self.show()

    @pyqtSlot()
    def on_click(self):
        print('PyQt5 button click')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
Masterly answered 23/8, 2017 at 13:58 Comment(1)
See hereShardashare
S
34

button.clicked.connect(self.on_click) binds signal of a button (clicked) to a slot self.on_click. Without this binding click will result in no action.

@pyqtSlot, in turn, is a decorator which converts simple python method to Qt slot. Doc states:

Although PyQt5 allows any Python callable to be used as a slot when connecting signals, it is sometimes necessary to explicitly mark a Python method as being a Qt slot and to provide a C++ signature for it. PyQt5 provides the pyqtSlot() function decorator to do this.

...

Connecting a signal to a decorated Python method also has the advantage of reducing the amount of memory used and is slightly faster.

When is that decorating necessary?

The only case I know is when you want to call methods from a QML interface: you can pass an object derived from QObject to QML and call object's methods but only if these methods are decorated with @pyqtSlot decorator with defined signature.

For your case consider it as a good practice - denoting a method as a slot for some signal.

System answered 23/8, 2017 at 14:12 Comment(2)
If you use QMetaObject.invokeMethod with a QuededConnection, then the method must be known to the Qt Meta Object system. Thus, I believe @pyqtSlot is necessary in this case as well. (Although there may be other ways to mark a method for the Qt Meta Object system.)Interflow
codeproject.com/articles/1123088/… investigates what "slightly faster" means and comes to the conclusion: use @pyqtSlot when generating "large numbers" of QObjects.. for substantial memory savings in such cases.Trinatte

© 2022 - 2024 — McMap. All rights reserved.