简体   繁体   English

信号能否获取 PySide/PyQt 中槽的返回值

[英]Can a signal get the return value of a slot in PySide/PyQt

Can I change the return type of a slot (such as class type or str or int)?我可以更改槽的返回类型(例如 class 类型或 str 或 int)吗? And if that is possible, can I return two value like a normal function (such as (bool, str) or (int, str) )?如果可能的话,我可以像普通的 function 一样返回两个值(例如(bool, str)(int, str) )吗?

Code:代码:

class SignalSlotTest(QObject):

    get_str = QtCore.Signal(str)
    get_bool_and_str = QtCore.Signal(str)

    def __init__(self):
        super().__init__()

    def start(self):
        msg = self.get_str.emit('start1')
        print(f"start1 return : {msg}")
        (is_valid, msg) = self.get_bool_and_str.emit('start2')
        print(is_valid, msg)


class MainTest(QObject):

    @QtCore.Slot(str, result = str)         #   question 1
    def get_str_func(msg):
        return ""
    
    @QtCore.Slot(str, result=(bool, str))   #   question 2
    def get_bool_and_str_func(msg, msg2):
        return (False,"return_message")

if  __name__ == "__main__":
    main_test = MainTest()
    signal_slot_test = SignalSlotTest()
    signal_slot_test.get_str.connect(main_test.get_str_func)
    signal_slot_test.get_bool_and_str.connect(get_bool_and_str_func)
    signal_slot_test.start()

When I tested the first question, return type is 'bool' type.当我测试第一个问题时,返回类型是 'bool' 类型。 (It prints True ). (它打印True )。

And when I tested the second question, an error occurs:而当我测试第二个问题时,出现错误:

TypeError: 'bool' object is not iterable

I don't know if my method is wrong...不知道是不是我的方法不对...

In PyQt and PySide, emit() will never return the return-value of any connected slot.在 PyQt 和 PySide 中, emit()永远不会返回任何已连接插槽的返回值。 PyQt always returns None , whilst PySide always returns True . PyQt 总是返回None ,而 PySide 总是返回True (I don't exactly know why PySide returns this value, or whether it could ever return False , as it doesn't seem to be documented anywhere). (我不完全知道 PySide为什么返回这个值,或者它是否可以返回False ,因为它似乎没有在任何地方记录)。

The signal-slot mechanism provides loosely-coupled notfications between unrelated objects.信号槽机制在不相关的对象之间提供松散耦合的通知。 Signals are broadcast entirely passively.信号完全被动地广播。 It does not matter whether they are connected to one slot, a dozen slots, or no slots at all.它们是连接到一个插槽、十几个插槽还是根本没有插槽都没有关系。 The slots simply react to the signals without needing to know anything about the object that emitted them.插槽只需对信号做出反应,而无需了解发出它们的 object 的任何信息。 Likewise, the behaviour of the slots is irrelevant to the signals that are connected to them.同样,插槽的行为与连接到它们的信号无关。

As for Qt itself, the docs state that:至于 Qt 本身,文档state 表示:

Signals are automatically generated by the moc and must not be implemented in the.cpp file.信号由 moc 自动生成,不得在 .cpp 文件中实现。 They can never have return types (ie use void).它们永远不能有返回类型(即使用 void)。

However, there are several questions on SO that prove that this is not entirely true.然而,有几个关于 SO 的问题证明这并不完全正确。 For example:例如:

And indeed, when I tested the example given in this answer , I found it still works as expected with the latest versions of both Qt5 and Qt6.事实上,当我测试这个答案中给出的示例时,我发现它仍然可以在 Qt5 和 Qt6 的最新版本中正常工作。 However, this kind of hackery has never been possible in PyQt/PySide.然而,这种黑客行为在 PyQt/PySide 中是不可能的。 Presumably, this is simply because there's no obvious use-case for it (ie nothing that can't be achieved by other means).据推测,这仅仅是因为它没有明显的用例(即没有什么是其他方式无法实现的)。

As for slots: of course it is possible to declare them to have any kind of return value in PyQt, PySide and also Qt itself.至于插槽:当然可以在 PyQt、PySide 以及 Qt 本身中声明它们具有任何类型的返回值。 Slots aren't only made for connections to signals.插槽不仅用于连接信号。 They can also be invoked directly, or by the meta object system , so there's no reason to limit their return values in any way.它们也可以直接调用,或者由元 object 系统调用,因此没有理由以任何方式限制它们的返回值。

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

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