[Development] How to skip write property in qml's property binding?

Mike Trahearn miketrahearn at imagineuisoftware.com
Fri Aug 9 01:55:26 CEST 2024


To Ulf’s point,

You would indeed not be able to restore a previous value using text: fooObject?.title ?? text because it will have been overwritten whenever fooObject?.title yields a valid value.
The most reasonable solution therefore is to use the Binding element which is designed to restore the previous/original binding or value of the binding target property whenever its “when” property is false.
It isn’t the most concise but it is bomb-proof and explicit, and is fully aligned to your requirements.


Text {
    id: yourTextObject

    text: "defaultString" // default binding when fooObject !== null; this could itself be a binding to some other default property.

    Binding { 
        yourTextObject.text: fooObject.title // assuming title will be valid if fooObject !== null, if not the “when” property might need fooObject?.title
        when: fooObject  // else it will be restored to being bound to the “defaultString" value
    }
}

Kind regards,

Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20240809/fcad11d2/attachment-0001.htm>


More information about the Development mailing list