2

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Hobin C.
  • 671
  • 1
  • 8
  • 17
  • I find a similar quesiton [Qt: meaning of slot return value?](https://stackoverflow.com/questions/112861/qt-meaning-of-slot-return-value). Their discussion has an assumption that using 'return' statement in a slot is fine. And my question is slightly different: Is this assumption is right? – Hobin C. Feb 27 '19 at 07:25

1 Answers1

2

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks for answering. I read the official artical (https://doc.qt.io/qt-5/signalsandslots.html) and still do not get any clue for this question. Could you possibly give me some references? – Hobin C. Feb 27 '19 at 03:54
  • @C.Hobin Is that there really is no reference about it, maybe a possible reference would be to see the source code. – eyllanesc Feb 27 '19 at 03:57
  • @C.Hobin Maybe https://doc.qt.io/qt-5/signalsandslots.html#signals: *When a signal is emitted, the slots connected to it are usually executed immediately, just like a normal function call* . So within the Qt source code there should be something similar to: `while(is_available_data) # other code if(is_signal_emitted(signal)) for slot in get_slots_by_signal(signal): slot()` – eyllanesc Feb 27 '19 at 03:59
  • I see your point. Actually, I am not expecting getting any value from where a signal is emitted. What I am curious about is the interruption caused by 'return' inside a 'slot'. Will this interruption have collision with the signal&slot mechanism? – Hobin C. Feb 27 '19 at 06:02
  • @C.Hobin 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. – eyllanesc Feb 27 '19 at 06:06
  • Your response can be true. In this case, the 'func_with_return' function may return None or other stuff implicitly if there is no 'return' statement. This idea might be the breakthrough for me. Anyway, thanks again for your patience. – Hobin C. Feb 27 '19 at 06:17