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

Binding does not restore original property value if it was assigned as a constant

    XMLWordPrintable

Details

    • f38e071f5b353cbf9ce6c6c104bd82099ae0aa14

    Description

      The Binding type only restores a property's original value if was set to an expression, and not a constant value.

      Take the following example:

      import QtQuick 2.0
      
      Rectangle {
          id: root
      
          property int count
          property bool enableBinding: true
      
          width: 200; height: 200
      
          color: "blue"           // Doesn't work, this color won't be restored when enableBinding=true
          //color: true ? "blue" : "blue"  // Works, this color will be restored when enableBinding=true
      
          Timer {
              interval: 800
              running: true
              repeat: true
              onTriggered: root.count += 1
          }
      
          Binding {
              id: binding
              target: root
              property: "color"
              value: count % 2 ? "green" : "yellow"
              when: enableBinding
          }
      
          focus: true
          Keys.onSpacePressed: {
              enableBinding = !enableBinding
          }
      }
      

      The Rectangle's color is initially set to blue, but the Binding object causes the Rectangle's color to alternate between green and yellow.

      When enableBinding=false, this binding is disabled, and the original blue color should be restored. However, this only happens if the initial color property value was set as an expression (e.g. "if (1) "blue") and not if it was set as a constant (e.g. "blue").

      This makes the Binding type unusable in the case where a library component needs to temporarily modify a provided object, as there is no guarantee that the object's original property value will be restored.

      Attachments

        Issue Links

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

          Activity

            People

              ulherman Ulf Hermann
              blammit Bea Lam
              Votes:
              3 Vote for this issue
              Watchers:
              17 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes