How to queue the invocation of a slot instead of calling directly ?



  • Dear all,
    with the QMetaObject from C++ it is possible to use the invokeMethod with the option to queue the invocation to the event thread instead of calling immediately.
    I'm wondering if there is an analogous mechanism on the QML side.

    Thanks,
    Gianluca.



  • Not directly. There was a WIP patch to add Qt.later() and Qt.after() functions to the Qt global object, to allow this use-case, but it couldn't handle the case where the context got deleted out from underneath it gracefully.

    The closest you can do is create a Timer object with a timeout of 1 millisecond (actually, 0 might work, not sure), and invoke the function in the onTriggered / onTimeout or whatever it's called, handler.

    @
    Timer {
    id: fnQueue
    property var queuedFunction
    interval: 1
    onTriggered: { queuedFunction.call() }
    }

    // some imperative code...
    fnQueue.queuedFunction = someFunc;
    fnQueue.start()
    @



  • [quote author="chrisadams" date="1402025578"]
    The closest you can do is create a Timer object with a timeout of 1 millisecond (actually, 0 might work, not sure), and invoke the function in the onTriggered / onTimeout or whatever it's called, handler.

    @[/quote]

    Agreed, without adding additional underlying code this is probably the best solution. An interval of 0 works for this too, in my experience.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.