Thank you, I need Text { Binding on text { value: fooObject.title; when: fooObject } }
But It's troublesome to write like this, I think the Binding is superfluous. Just like how, when detecting that the value is `undefined`, it tries to reset the property (if it has a corresponding reset function) or reports "Unable to assign [undefined] to QString," could we add a special value that, when detected, does nothing? This would be less costly than using Binding to achieve this effect, and we are already doing something similar for the special value `undefined`. ________________________________ From: Fabian Kosmale <fabian.kosm...@qt.io> Sent: Friday, July 19, 2024 14:37 To: JiDe Zhang <zc...@live.com>; Qt邮件列表 <development@qt-project.org> Subject: AW: How to skip write property in qml's property binding? Hi, 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 } } Compare https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdoc.qt.io%2Fqt-6%2Fqml-qtqml-binding.html%23conditional-bindings&data=05%7C02%7C%7Ceb6b834d4f2d4f8c493808dca7bd4e7c%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638569678723442236%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=6FLxZhLwF96F13r%2Bdm%2BwPXi4G5w4chdANcCyky8n1yg%3D&reserved=0<https://doc.qt.io/qt-6/qml-qtqml-binding.html#conditional-bindings>. Set restoreMode depending on your needs. If you don't need an actual binding, but only the value, then you can use Text { id: mytext Component.onCompleted: { if (fooObject) mytext.text = fooObject.name } } if the nullishness of fooObject never changes. If it can change, you need to react to it with a Connection element, a connection, or a property change handler (depending on where the object is defined/how it is exposed to QML) or you again fall back to Binding. An alternative would be to take a step back, and to consider why you don't want to have the write to begin with. If you want to preserve a default value, you might want to consider exposing that one, too, so that you can write: // MyText.qml Text { property string defaultText: "some value"; text: defaultTtext } // Usage.qml MyText { id: myText; text: fooObject?.title ?? myText.defaultText } This of course assumes that you have control over the item setting the default value. Kind regards, Fabian Kosmale ________________________________________ Von: Development <development-boun...@qt-project.org> im Auftrag von JiDe Zhang <zc...@live.com> Gesendet: Freitag, 19. Juli 2024 07:31 An: Qt邮件列表 Betreff: [Development] How to skip write property in qml's property binding? For an example: Text { text: fooObject?.title } When the fooObject is null, will got a qml error "Unable to assign [undefined] to QString". I don't want the error, so, how to skip write value for "text" property when fooObject is null? Text { text: fooObject?.title ?? text } This is a workaround, but it is a magic, maybe will got a cycle binding? Text { text: fooObject?.title ?? bypass objectName: { if (fooObject) return fooObject.name bypass } } This syntax will more easily understand.
-- Development mailing list Development@qt-project.org https://lists.qt-project.org/listinfo/development