How to assign property values temporarily without breaking existing binding in qml?



  • I have TextField whose text is bound with another value. However, when I press cancel of the virtual keyboard I want to revert its value and this value is stored in another variable. However, the following code breaks existing binding of TextField's text with the other value.

    Keys.onCancelPressed:
    {
          text = previousNumber
          focus = false
    }
    

    How do I make sure that the previous binding is kept intact?

    Thanks.


  • Moderators

    @red.green said in How to assign property values temporarily without breaking existing binding in qml?:

    How do I make sure that the previous binding is kept intact?

    Assigning a value breaks the binding - if it was not this way QML would be horribly broken ;-)

    There are a few ways around it, though. First, most obvious one - set the new value on the binding source. So if you have:

    Text {
      text: someProperty
    } 
    

    Assign your temporary value to someProperty. You can add some code that will manage it's temporary nature there (onPropertyChanged or better in some object that holds the value of that property).

    Another way is to set up the binding using Binding element, disable it when Cancel is pressed, then enable it again once your temporary situation changes.



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