Context property function calls and threading
In our application, we have a C++ object embedded in QML using the standard approach. Before setting this context property, the object is moved to its own worker thread:
pTheObject->moveToThread(pWorkerThread); pQmlEngine->rootContext()->setContextProperty("theObject", pTheObject);
We declare the functions we want to call directly from QML with Q_INVOKABLE:
Q_INVOKABLE void someMethod();
and can call them from QML without any problems:
Here's the problem: when calling someMethod from QML, it's executed on the GUI thread, rather than the worker thread. This has the effect that calls within someMethod which are supposed to update the GUI have no visible effect, because the GUI thread is busy doing someMethod's work for it.
When using the signals and slots approach with Qt::QueuedConnection, rather than the context property approach, someMethod is executed on the worker thread. But, using the context property approach is much nicer, as it makes the source code much simpler, because we don't have to manually call QObject::connect for all connections.
Is there a way to get around this?
Not a direct answer but there's a QML threading example here that might give you a starting point.
Hope it helps
My making the function Q_INVOKABLE, you are making function eligible to call it through MetaObject system. Change ownership of object has no effect on this function call. This has no effect because it is like direct execution of the function.
If you make the call through signal&slots, it like async(across thread) call. So when you move the object, slots gets executed in the context of owner thread.
Hope this helps.
Thanks for the responses - really good info. Think I'll investigate the QML worker thread option.