13
votes

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?

4
The equivalent would be Timer. Set repeat: false to get single shot behavior.Pavel Strakhov
repeat is false by default, actually.hyde
Nice question. setTimeout/setInterval are out of question since they cannot be used. You can consider this answer as a - rather hackish - approach. By setting parameters in function signature you can reduce the usage to a single line, e.g. delay(/*repeat*/ true, 12000, functionName).BaCaRoZzo

4 Answers

11
votes

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 () { ... } );
10
votes

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
            }
        }
    }
}
3
votes

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()
        }
    }
} 
1
votes

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