Qt: meaning of slot return value?

后端 未结 4 1611
粉色の甜心
粉色の甜心 2020-12-06 04:32

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

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 04:50

    All slots are exposed in QMetaObject, where the object can be accessed via a reflective interface.

    For instance, QMetaObject::invokeMethod() takes a QGenericReturnArgument parameter. 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.)

    The invokeMethod function is, for example, used by various dynamic languages such as QML and Javascript to call methods of 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.)

    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:

    Same as QueuedConnection, except the current thread blocks until the slot returns. 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.

提交回复
热议问题