Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a QObject function from QML across threads

I'm trying to determine how calling QObject slots or Q_INVOKABLE methods from QML for a QObject that lives in another thread works, and whether or not its safe to do so.

Assume there's a MainThread and ThreadA. QObjectA lives in ThreadA. The QML engine/gui/everything lives in the MainThread. I expose QObjectA to the QML engine using

declarativeView->setContextProperty("someObj",ObjectA)

Now in a QML file, I call

someObj.someMethod();

Where someMethod is a slot or is Q_INVOKABLE. I'd like to know which thread actually executes the function. If it's MainThread, that would be a bad thing, and calling a method like that across threads would be dangerous. If it was executed by ThreadA however, all would be well.

Based on this documentation: http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html, I'm assuming that QMetaObject::invokeMethod() is used to call the QObject function. That documentation (http://doc.qt.nokia.com/4.7-snapshot/qmetaobject.html#invokeMethod), shows that there are different connection types available, just like with Qt signals and slots.

I'd like to know if Qt's qml engine automagically chooses the right type for the situation when invoking C++ methods from QML across threads, and if so, calling methods for objects that live in other threads from QML is an acceptable practice.

like image 360
Prismatic Avatar asked Aug 20 '12 00:08

Prismatic


1 Answers

As it became apparent a while ago, QML doesn't seem to be able to go across threads.

So one needs to implement a C++ side intermediate object that lives in the main thread to dispatch calls to objects in other threads.

QML object -> object in a different thread // doesn't work!!!
QML object -> C++ mediator object -> object in a different thread // WORKS!!!

Basically, "transcending" threads must happen in C++ entirely, thus the need of a mediator object.

like image 133
dtech Avatar answered Nov 16 '22 07:11

dtech