How to replace setInterval



  • 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 ?



  • Does this help?

    @Rectangle {
    width: 200
    height: 50

    Timer {
        interval: 1000
        running: true
        repeat: true
        onTriggered: clock.text = Qt.formatTime(new Date())
    }
    
    Text {
        id: clock
        anchors.centerIn: parent
    }
    

    }@



  • 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).



  • You could just as well call a function when the onTriggered signal is emitted. Any JS expression can go there.



  • How can I get acces to draw() from qml Timer?



  • Here is the same sample that uses a function instead:

    @Rectangle {
    width: 200
    height: 50

    Timer {
        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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.