Home > database >  Are QML calls to C public slots executed in the c target thread, or in QML (main thread)?
Are QML calls to C public slots executed in the c target thread, or in QML (main thread)?

Time:04-30

Imagine we have a QObject-based c class called CppClass that is registered in the QML context with the name qmlCppClass and moved to a new thread using QObject::moveToThread("newThread* name").

Imagine CppClass has a public slot declared as void doSomething().

Imagine the root QML object/window has a signal defined inside it called signal().

Now assume 3 cases:

  1. Inside my main.cpp (where I create the QML engine) I connect the signal() from QML to doSomething() in c in this way
    QObject::connect("QObject* cast of my QML engine", SIGNAL(signal()), "CppClass* object name", SLOT(doSomething()));
  1. Inside my QML code, I write this inside the onClicked() slot in a button qml type:
    qmlCppClass.doSomething()
  1. I use the Connections type in QML like this:
    Connections {
        target: qmlRoot
        function onSignal() {
            qmlCppClass.doSomething()
        }
    }

In each case, is the slot executed in the qml thread (a.k.a. main thread) or is it executed in the thread containing the CppClass object instance (a.k.a. target thread)?

CodePudding user response:

If connect is done with QueuedConnection then the slot will be executed on the receiver's thread, otherwise—on the sender's. Given you moved the receiver to another thread before making the connection, it would be a queued connection by default.

If there is no direct call to the connect function then invokeMethod might be used which has the same behavior as described above.

And if the direct call to a member function is used then it is executed in the thread where it is called from.

  • Related