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

Pierre-Yves Siret py.siret at gmail.com
Fri Jul 19 11:09:40 CEST 2024


>
> as always, it depends. If  you really want to set a binding based on a
> condition, but otherwise leave the value as it is, you want to use
>
> Text {
>      Binding on text {  value: fooObject.title; when: fooObject }
> }
>

Note that the `when` only affects when the binding is applied to the target
property, not when the value property is evaluated.
The above code will result in "Cannot read property 'title' of null" when
fooObject is null, cf. https://bugreports.qt.io/browse/QTBUG-22005

Maybe work could resume on attempting to find a solution on this issue?

Meanwhile I think the best solution is `text: fooObject?.foo ?? ""`
This does assign an empty string but I think it does make sense and it is
clearer in this case.
For custom and complex properties you could use `undefined` instead and
potentially have a `RESET` function for a c++ defined `Q_PROPERTY`.

For more advanced use cases where you don't want to reset the value, use
Binding and restoreMode.

Cheers,
Pierre-Yves Siret
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20240719/2ddd8cb4/attachment.htm>


More information about the Development mailing list