Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-39371

QML Timers can fire at "arbitrary" times

    XMLWordPrintable

Details

    • ca43c4121dde6c8f5d4eabfcf128ff6214996d54

    Description

      Timers are ticked by the animation system, and when a timer is about to fire, if another animation happens to stop, the timer will fire right away, which might result in leaving the state of the UI in a bad state.

      This is a bit similar to applications calling QCoreApplication::processEvents() which is highly discouraged for the same reason. In this case, we can fix the issue by moving the timer triggering to be driven by a queued invoke rather than a direct connection.

      import QtQuick 2.0
      
      Rectangle {
          width: 360
          height: 360
          property bool bonus
      
          MouseArea {
              anchors.fill: parent
              onPositionChanged: {
                  bonus = true
                  for (var i=0; i<500; i++)
                      r.x = Math.random() * 340
                  bonus = false
              }
          }
      
          Rectangle {
              id: r
              width: 20
              height: 20
              color: "blue"
              Behavior on x { NumberAnimation {} }
          }
      
          Timer {
              interval: 1
              repeat: true
              running: true
              onTriggered: if (bonus) { console.warn("oops"); console.trace() }
          }
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            sletta Gunnar Sletta
            sletta Gunnar Sletta
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes