Qt: meaning of slot return value?
Asked Answered
B

5

27

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?


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;
Brasilin answered 22/9, 2008 at 2:12 Comment(0)
F
19

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. Although it seems like it's a bit more complicated to do.

Flowered answered 30/9, 2008 at 17:25 Comment(0)
B
12

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)

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

using 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.

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. 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.

Britnibrito answered 22/9, 2008 at 3:0 Comment(0)
S
7

It is Very useful when you deal with dynamic language such qtscript JavaScript QtPython and so on. With this language/bindings, you can use C++ QObject dinamically using the interface provided by MetaObject. As you probably know, just signals and slots are parsed by moc and generate MetaObject description. 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. Often Qt bindings for dynamic languages provides some facility to acces to normal method, but the process is definitely more triky.

Sideman answered 1/2, 2010 at 14:59 Comment(1)
take a look to QWebFrame::addToJavaScriptWindowObject and think about how to let the javascript in that frame to get data from object you added.Sideman
D
5

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.

Deutero answered 7/1, 2011 at 13:31 Comment(0)
M
1

Well this question was first asked 14 years ago. Now QML allows Slots to return a value and that is for sure.

You have to declare the type of the return value using the result parameter (Notice that it is return but rather result).

In python, giving two numbers to a Slot and accepting a value, the syntax will be

@pyqtSlot(int, int, result=int)
def add(x,y):
    return x + y

You can call this as

ret_val = add(1, 2)
Mchail answered 8/5, 2023 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.