pythonpyqtpyqt5qt-signalsqt-slot

What effect will exist when using any 'return' statement inside Qt's slot


For instance, I connect the 'clicked' signal of QPushButton to a function named 'func_with_return'. Assumes that there are just three statements in this function: The first one is 'print('start')', the second one is 'return 1' and the last one is 'print('end')'. There is my python code based on PyQt5.

import sys
from PyQt5.QtWidgets import QApplication, QFrame, QPushButton

class MyWindow(QFrame):    
    def __init__(self):
        super(MyWindow, self).__init__()
        self.layout_init()
        self.layout_manage()

    def layout_init(self):
        self.setFixedSize(800, 600)
        self.button01 = QPushButton('click!', self)
        self.button01.setFixedSize(100, 100)
        self.button01.clicked.connect(self.func_with_return)

    def layout_manage(self):
        pass

    def func_with_return(self):
        print('---------func_with_return starts---------')
        return 1
        print('---------func_with_return ends---------')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mywindow = MyWindow()
    mywindow.show()
    sys.exit(app.exec_())

Basically, there is no error after clicking on this button. What I am curious about is the interruption caused by 'return' inside a 'slot'. Will this interruption have collision with the signal&slot mechanism?


Solution

  • None. The signals only invoke the function, if the function returns Qt will not use it.

    On the other hand in Qt/PyQt it is said that a function is a slot if you use the decorator @QtCore.pyqtSlot(). In your case it is a simple function. Even so for a signal will not serve the data that returns the slot or function invoked.

    Will this interruption have collision with the signal&slot mechanism?

    No, it does not have a collision. Returning to the beginning, middle or end is irrelevant, remember every function returns something (if you do not use return the function will implicitly return None at the end).


    On the other hand in a GUI the tasks of the functions must be light, if they are heavy you must execute it in another thread.