简体   繁体   English

在 Qt 的插槽中使用任何“return”语句时会产生什么影响

[英]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'.例如,我将 QPushButton 的“clicked”信号连接到名为“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')'.假设这个函数中只有三个语句:第一个是'print('start')',第二个是'return 1',最后一个是'print('end')'。 There is my python code based on PyQt5.有我基于 PyQt5 的 python 代码。

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?这个中断会不会和信号槽机制发生冲突?

None.没有任何。 The signals only invoke the function, if the function returns Qt will not use it.信号只调用函数,如果函数返回 Qt 将不会使用它。

On the other hand in Qt/PyQt it is said that a function is a slot if you use the decorator @QtCore.pyqtSlot() .另一方面,在 Qt/PyQt 中,如果您使用装饰器@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).回到开头、中间或结尾无关紧要,记住每个函数都会返回一些东西(如果你不使用 return 函数将在结尾隐式返回 None )。


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.另一方面,在 GUI 中,函数的任务必须是轻量级的,如果它们很重,则必须在另一个线程中执行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM