Qt:slot返回值的意义? [英] Qt: meaning of slot return value?

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

问题描述

根据文档,从槽的返回值并不意味着任何东西。

然而在生成的moc代码中,我看到如果一个槽返回一个值,这个值被用于某事。任何想法,它做什么?

According to the documentation the return value from a slot doesn't mean anything.
Yet in the generated moc code I see that if a slot returns a value this value is used for something. Any idea what does it do?


关于。这是从moc生成的代码。 'message'是一个不返回任何内容的槽,'selectPart'被声明为返回int。

Here's an example of what I'm talking about. this is taken from code generated by moc. 'message' is a slot that doesn't return anything and 'selectPart' is declared as returning int.

case 7: message((*reinterpret_cast< const QString(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2]))); break;
case 8: { int _r = selectPart((*reinterpret_cast< AppObject*(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2])));
    if (_a[0]) *reinterpret_cast< int*>(_a[0]) = _r; }  break;


推荐答案

是从QMetaObject :: invokeMethod调用的返回类型可以指定和返回值获得。 (看看在Qt帮助中的invokeMethod)

Looking through the Qt source it seems that when a slot is called from QMetaObject::invokeMethod the return type can be specified and the return value obtained. (Have a look at invokeMethod in the Qt help)

我找不到这个实际上在Qt源中使用的很多例子。一个我发现是

I could not find many examples of this actually being used in the Qt source. One I found was

bool QAbstractItemDelegate::helpEvent

这是一个具有返回类型并且从

which is a slot with a return type and is called from

QAbstractItemView::viewportEvent

使用invokeMethod。

using invokeMethod.

我认为一个插槽的返回值只有在直接调用函数(当它是一个普通的C ++函数时)或者使用invokeMethod时才可用。

I think that the return value for a slot is only available when the function is called directly (when it is a normal C++ function) or when using invokeMethod. I think this is really meant for internal Qt functions rather than for normal use in programs using Qt.

编辑:
对于示例情况:

For the sample case:

case 8: { int _r = selectPart((*reinterpret_cast< AppObject*(*)>(_a[1])), *reinterpret_cast< int(*)>(_a[2])));
if (_a[0]) *reinterpret_cast< int*>(_a[0]) = _r; }  break;

向量_a是传递给qt_metacall的参数列表。这由QMetaObject :: invokeMethod传递。因此,moc生成的代码中的返回值被保存并传回给调用者。因此,对于正常的信号时隙交互,返回值不用于任何东西。但是,存在这种机制,如果通过invokeMethod调用该槽,则可以访问来自槽的返回值。

the vector _a is a list of arguments that is passed to qt_metacall. This is passed by QMetaObject::invokeMethod. So the return value in the moc generated code is saved and passed back to the caller. So for normal signal-slot interactions the return value is not used for anything at all. However, the mechanism exists so that return values from slots can be accessed if the slot is called via invokeMethod.

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

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