Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a return value from QMetaObject::invokeMethod

Tags:

c++

qt

I am calling from my thread the following:

QMetaObject::invokeMethod(pProcessor,
                          "doTask",
                          Qt::QueuedConnection,
                          Q_RETURN_ARG(quint32, taskId),
                          Q_ARG(quint64,   objId),
                          Q_ARG(quint8,    intId),
                          Q_ARG(QString,   name),
                          Q_ARG(QString,   comment)
                          );

but it just fails, no matter what I do. If I take out Q_RETURN_ARG(quint32, taskId), the method is invoked, but I need the taskId, which I cannot get. Any help is much appreciated.

like image 475
Amy Avatar asked Sep 10 '13 17:09

Amy


1 Answers

I'm assuming you want to call a method of an object from non owner thread & wants to get the return value. In order to do that use "Qt::BlockingQueuedConnection" as a connection type.

quint32 taskId; // Declare taskId.
qRegisterMetaType<quint32>("quint32");
QMetaObject::invokeMethod(pProcessor,
                      "doTask",
                      Qt::BlockingQueuedConnection,
                      Q_RETURN_ARG(quint32, taskId),
                      Q_ARG(quint64,   objId),
                      Q_ARG(quint8,    intId),
                      Q_ARG(QString,   name),
                      Q_ARG(QString,   comment)
                      );

Incase your method returns non standard return type you have to register your type before calling QMetaObject::invokeMethod(...). Refer http://qt-project.org/doc/qt-5.0/qtcore/qmetatype.html#qRegisterMetaType.

like image 120
Arunprasad Rajkumar Avatar answered Oct 21 '22 11:10

Arunprasad Rajkumar