How to replace setInterval
-
wrote on 21 Mar 2013, 11:23 last edited by
Hi there,
I've got problem with rewriting javascript (HTML 5 Canvas) project into Qml.
I don't know how to replace function setInterval from JavaScript into QML implementation.How can I solve that ?
Maybe someone have ready example ? -
wrote on 21 Mar 2013, 11:33 last edited by
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 }
}@
-
wrote on 21 Mar 2013, 12:13 last edited by
Unfortunately no.
Maybe I show it on example:@Canvas{
onPaint:fun();
function fun() { function draw() { } }
}@
I would like to use Timer for draw function (instead of setInterval, becouse it isn't supported).
-
wrote on 21 Mar 2013, 12:20 last edited by
You could just as well call a function when the onTriggered signal is emitted. Any JS expression can go there.
-
wrote on 21 Mar 2013, 12:27 last edited by
How can I get acces to draw() from qml Timer?
-
wrote on 21 Mar 2013, 12:36 last edited by
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()) }
}@
-
wrote on 30 Jan 2014, 11:18 last edited by
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.