QTimer::singleShot equivalent for QML

6.1k views Asked by At

Consider this C++ statement (example from docs):

QTimer::singleShot(600000, &app, SLOT(quit()));

How to do the same in .qml JavaScript, something like this QML:

Rectangle {
    property int counter: 0
    onCounterChanged: {
        if (counter > 42) {
            // do equivalent of above C++ statement here
        }
    }
    // more code, which actually manipulates counter 
}

There's the obvious solution of having separate Timer, which is then started by this JavaScript code, and I'll accept that as an answer if a one-liner is not possible. Is it?

6

There are 6 answers

2
Luca Carlon On BEST ANSWER

Another option I came up with is to simply define a function like this in C++:

void QmlUtils::singleShot(int msec, QJSValue callback)
{
    QTimer::singleShot(msec, this, [callback] () mutable {
        if (callback.isCallable())
            callback.call();
    });
}

and I call it from QML with:

lqtUtils.singleShot(5000, () => console.log("Hello!"))

In the callback you can access QML elements:

let abc = "Hello Luca!"
lqtUtils.singleShot(10000, function() {
    console.log(abc)
    myRect.color = "orange"
})
    
[...]
    
Rectangle {
    id: myRect
    anchors.fill: parent
    color: "red"
}

I then added that C++ function to my collection of "must-have" here: https://github.com/carlonluca/lqtutils/blob/master/lqtutils_ui.h#L53.

0
synacker On

Change "repeat" property to false for Timer object.

import QtQuick 1.0

Item {
    Timer {
        id: timer
        interval: 600000
        running: false
        repeat: false
        onTriggered: Qt.quit()
    }

    Rectangle {
        property int counter: 0
        onCounterChanged: {
            if (counter > 42) {
                timer.running = true
            }
        }
    }
}
0
Sherif O. On

there is a timer component in QML

import QtQuick 2.0

Item {
    Timer {
        interval: 500; running: true; repeat: true
        onTriggered: time.text = Date().toString()
    }

    Text { id: time }
}

for more details see the documentation

0
Joe On

I set properties like this running: true; repeat: false;

    Timer {
      interval: 5000 
      running: true
      repeat: false
      onTriggered:console.log("Test");
    }
0
Ivan Kolev On

I ended up adding this to my main.qml:

Component {
    id: delayCallerComponent
    Timer {
    }
}

function delayCall( interval, callback ) {
    var delayCaller = delayCallerComponent.createObject( null, { "interval": interval } );
    delayCaller.triggered.connect( function () {
        callback();
        delayCaller.destroy();
    } );
    delayCaller.start();
}

Which can be used like this:

delayCall( 1000, function () { ... } );
0
pixelgrease On

Here is how to do it using a SequentialAnimation element:

SequentialAnimation {
    id: quitTimer
    PauseAnimation { duration: 60000 }
    ScriptAction { script: Qt.quit() }
}

Rectangle {
    property int counter: 0
    onCounterChanged: {
        if (counter > 42) {
            quitTimer.start()
        }
    }
}

If that's too ugly, make a component out of it:

// SingleshotTimer.qml
import QtQuick 2.0
SequentialAnimation {
    property alias delay: delayAnim.duration
    property alias script: scriptAction.script

    PauseAnimation { id: delayAnim; duration: 10000 }
    ScriptAction { id: scriptAction }
}

Using this new component gives what you want:

SingleshotTimer { id: timer; delay: 60000; script: Qt.quit() }

Rectangle {
    property int counter: 0
    onCounterChanged: {
        if (counter > 42) {
            timer.start()
        }
    }
}