Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to replace setInterval
Forum Updated to NodeBB v4.3 + New Features

How to replace setInterval

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 7.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MMSS
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • U Offline
      U Offline
      utcenter
      wrote on last edited by
      #2

      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
      }
      

      }@

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MMSS
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • U Offline
          U Offline
          utcenter
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MMSS
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • U Offline
              U Offline
              utcenter
              wrote on last edited by
              #6

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

              }@

              1 Reply Last reply
              0
              • H Offline
                H Offline
                heatblazer
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved