How to replace setInterval
QML and Qt Quick
7
Posts
3
Posters
7.8k
Views
1
Watching
-
Does this help?
@Rectangle {
width: 200
height: 50Timer { interval: 1000 running: true repeat: true onTriggered: clock.text = Qt.formatTime(new Date()) } Text { id: clock anchors.centerIn: parent }
}@
-
Here is the same sample that uses a function instead:
@Rectangle {
width: 200
height: 50Timer { interval: 1000 running: true repeat: true onTriggered: setTime() } Text { id: clock anchors.centerIn: parent } function setTime() { clock.text = Qt.formatTime(new Date()) }
}@
-
I am a complete newbie in QT/QML but I was doing something recently, and came with that solution. In your specific QML file which can be named "setInterval.qml" :
@Timer {
interval: 100; //ms
running: true;
repeat: true;
signal onTriggeredState;
onTriggered: onTriggeredState();
}
@then in the javascript file you can do:
@
var timer = Qt.createQmlObject("setInterval.qml", attachedToObject);
var ctx = Qt.createComponent(timer);
timer.onTriggeredState.connect( (function() { console.log('foobar');})());
@
Of course you can add this to your logic methods or constructors for reuse. It worked for me.