Important: Please read the Qt Code of Conduct -

Synchronous signals between C++ and QML

  • I'm running into a problem (limitation) when firing signals between C++ and QML code - here's my scenario:

    1. A QML UI event (mouse click) fires a signal that is being listened to by some C++ object.
    2. The C++ objects, in turn, fire some more signals which are being listened to by QML objects (the bindings for these signals are done in QML).

    I'm seeing, in #2, that when I emit the signals in C++ they are NOT being processed immediately in QML. As far as I can tell, they are using something equivalent to QueuedConnection, e.g. they are being processed during the next run through the event loop. I see my c++ emit return immediately, and when the QML-side code responds, I see it doing so from a stack For me, this is a problem, because it leaves both my front-end and back-end in an unresolved state: I need all of the connected signals to be processed before I can move on.

    In C++ Qt code, this can easily be resolved by doing something like: QObject::connect(sender, "syncSignal", receiver, "onSignal", Qt::DirectConnection). But - as far as I can tell the QML Connection object doesn't support specifying connection types.

    Is there a way I can synchronously signal QML code from C++? Is there another way of generating a binding in QML that might allow this? It appears that, once I've crossed the boundary into C++ code at all, it's impossible for me to jump back to QML without losing my stack context. Am I missing something obvious on the QML side here? Is there a way I can force a signal to be handled synchronously?

  • I don't know about signals. But from QML you can pass a Javascript function as callback to your C++ function (using a QJSValue argument). Calling that callback from C++ back to QML should be synchronous (well I think it is, never checked though).

Log in to reply