PySide:从插槽返回值 [英] PySide: Returning a value from a slot

查看:52
本文介绍了PySide:从插槽返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pyside 文档 指出插槽可以有返回值,但我还没有弄清楚如何让它这样做.

The Pyside documentation states that slots can have return values, but I have yet to figure out how make it do so.

我找不到任何可用的代码示例,所以我按照文档的建议添加了一个装饰器:

I couldn't find any usable code samples on this, so I've added a decorator as the docs suggest:

def testSlot(self):
    print('testSlot called')
    return 'RETURN VALUE RETURN VALUE RETURN VALUE'

我正在尝试像这样捕获返回值:

And I'm trying to capture the return value like this:

ret = foo.testSignal.emit()

但它不起作用.我的测试代码,你可以在这里找到,也可以在崩溃,出现这个错误输出.感谢帮助!

But it's not working. My test code, which you can find here, also crashes with this error output. Help is appreciated!

我删除了装饰器,它解决了崩溃问题,但它仍然没有返回我希望它返回的值.

I've removed the decorator, which solves the crashing issue, but it still doesn't return the value I want it to return.

按原样运行脚本给我这个:

Running the script as is give me this:

foo.testSignal.emit()
testSlot 被调用
真的

foo.testSignal.emit()
testSlot called
True

推荐答案

恐怕你的想法是不可能的.信号和槽松散耦合在一起,因此发出信号不会直接调用槽,因此无法检索其"返回值.

I'm afraid what you think is not possible. Signals and slots are loosely coupled together, so emitting a signal does not directly call a slot, and therefore cannot retrieve "its" return value.

如果您考虑一下,这可能会更清楚:

This might be clearer if you think about this:

您可以连接 2 个不同的槽(例如 2 个不同的类),它们返回不同的值,连接到一个信号.两个返回值中的哪一个应该到达您尝试分配的变量?

You can connect 2 different slots (of e.g. 2 different classes), which return different values, to one signal. Which of the two return values are supposed to reach the variable you try to assign?

你可能指的是这个:这是完全不同的东西,它允许通过名称"通过 MetaObject 系统调用"方法来返回值.

What you probably mean is this: This is something completely different, and allows return values via "invoking" the method over the MetaObject system by "name".

这篇关于PySide:从插槽返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆