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?
[static] void QTimer::singleShot(int msec, const QObject *receiver, const char *member) This static function calls a slot after a given time interval. It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.
The QTimer class provides repetitive and single-shot timers. The QTimer class provides a high-level programming interface for timers. To use it, create a QTimer, connect its timeout() signal to the appropriate slots, and call start(). From then on it will emit the timeout() signal at constant intervals.
To stop call QTimer::stop . To restart just call QTimer::start .
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 () { ... } );
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
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With