简体   繁体   English

Qt:槽返回值的含义?

[英]Qt: meaning of slot return value?

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. 然而在生成的moc代码中,我看到如果一个槽返回一个值,则该值用于某些东西。 Any idea what does it do? 知道它做了什么?


Here's an example of what I'm talking about. 这是我正在谈论的一个例子。 this is taken from code generated by moc. 这是从moc生成的代码中获取的。 'message' is a slot that doesn't return anything and 'selectPart' is declared as returning int. 'message'是一个不返回任何内容的插槽,'selectPart'被声明为返回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;

The return value is only useful if you want to call the slot as a normal member function: 仅当您要将插槽作为普通成员函数调用时,返回值才有用:

class MyClass : public QObject {
    Q_OBJECT
public:
    MyClass(QObject* parent);
    void Something();
public Q_SLOTS:
    int Other();
};

void MyClass::Something() { int res = this->Other(); ... }

Edit: It seems that's not the only way the return value can be used, the QMetaObject::invokeMethod method can be used to call a slot and get a return value. 编辑:似乎这不是返回值的唯一使用方式,QMetaObject :: invokeMethod方法可用于调用插槽并获取返回值。 Although it seems like it's a bit more complicated to do. 虽然看起来这样做有点复杂。

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. 通过Qt源看来,当从QMetaObject :: invokeMethod调用一个槽时,可以指定返回类型并获得返回值。 (Have a look at invokeMethod in the Qt help) (看看Qt帮助中的invokeMethod)

I could not find many examples of this actually being used in the Qt source. 我在Qt源代码中找不到很多实际使用的例子。 One I found was 我发现的是

bool QAbstractItemDelegate::helpEvent 

which is a slot with a return type and is called from 这是一个返回类型的槽,从中调用

QAbstractItemView::viewportEvent

using invokeMethod. 使用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. 我认为插槽的返回值仅在直接调用函数时(当它是普通的C ++函数时)或使用invokeMethod时才可用。 I think this is really meant for internal Qt functions rather than for normal use in programs using Qt. 我认为这真正意味着内部Qt功能,而不是在使用Qt的程序中正常使用。

Edit: 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;

the vector _a is a list of arguments that is passed to qt_metacall. vector _a是传递给qt_metacall的参数列表。 This is passed by QMetaObject::invokeMethod. 这是由QMetaObject :: invokeMethod传递的。 So the return value in the moc generated code is saved and passed back to the caller. 因此,moc生成的代码中的返回值被保存并传递回调用者。 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. 但是,存在这样的机制,以便在通过invokeMethod调用槽时可以访问槽中的返回值。

It is Very useful when you deal with dynamic language such qtscript JavaScript QtPython and so on. 当你处理动态语言如qtscript JavaScript QtPython等时非常有用。 With this language/bindings, you can use C++ QObject dinamically using the interface provided by MetaObject. 使用这种语言/绑定,您可以使用MetaObject提供的接口使用C ++ QObject。 As you probably know, just signals and slots are parsed by moc and generate MetaObject description. 您可能知道,只有信号和插槽由moc解析并生成MetaObject描述。 So if you are using a C++ QObject from a javascript binding, you will be able to call just slots and you will want the return value. 因此,如果您使用来自javascript绑定的C ++ QObject,您将只能调用插槽,并且您将需要返回值。 Often Qt bindings for dynamic languages provides some facility to acces to normal method, but the process is definitely more triky. 通常,动态语言的Qt绑定提供了一些访问普通方法的工具,但这个过程肯定更加模糊。

All slots are exposed in QMetaObject, where the object can be accessed via a reflective interface. 所有插槽都在QMetaObject中公开,可以通过反射接口访问对象。

For instance, QMetaObject::invokeMethod() takes a QGenericReturnArgument parameter. 例如, QMetaObject :: invokeMethod()采用QGenericReturnArgument参数。 So I belive this is not for explicit slot usage, but rather for dynamic invocation of methods in general. 所以我相信这不是用于显式插槽使用,而是用于一般的方法的动态调用。 (There are other ways to expose methods to QMetaObject than making them into slots.) (还有其他方法可以将方法暴露给QMetaObject,而不是将它们放入插槽中。)

The invokeMethod function is, for example, used by various dynamic languages such as QML and Javascript to call methods of QObject:s . 例如, invokeMethod函数被各种动态语言(如QML和Javascript)用于调用QObject:s方法。 (There's also a Python-Qt bridge called PythonQt which uses this. Not to be confused with PyQt , which is a full wrapper.) (还有一个名为PythonQt的Python-Qt桥使用它。不要与PyQt混淆,后者是一个完整的包装器。)

The return value is used when making syncrhonous calls across threads inside a Qt application (supported via invokeMethod and setting connection type to Qt::BlockingQueuedConnection , which has the following documentation: 在Qt应用程序内的线程之间进行同步调用时使用返回值(通过invokeMethod支持并将连接类型设置为Qt::BlockingQueuedConnection ,其中包含以下文档:

Same as QueuedConnection, except the current thread blocks until the slot returns. 与QueuedConnection相同,除了当前线程阻塞直到槽返回。 This connection type should only be used where the emitter and receiver are in different threads. 此连接类型仅应在发射器和接收器位于不同线程中的情况下使用。 Note: Violating this rule can cause your application to deadlock. 注意:违反此规则可能会导致应用程序死锁。

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

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