Setter function for internal qml object
Solved
QML and Qt Quick
-
wrote on 28 Jun 2021, 14:52 last edited by Moisi
Hi,
I have a component (
MyItem.qml
) which is an Item with another Item with a value inside. Like this:Item { readonly property Item wrapper: private.wrapper // this readonly property is important. function setValue( newValue ) { private.wrapper.value = newValue; } Item { id: private property Item wrapper: Item { property int value: 42 } } }
Now imagine that I do not want to allow user to change the
value
property without using the setter function. For example:Item { id: root MyItem { id: myItem } Column { Button { text: "allowed"; onClicked: myItem.setValue(13) } Button { text: "not allowed"; onClicked: myItem.wrapper.value = 13 } // This should not be allowed } }
Does someone have any trick or pattern to do this ?
-
wrote on 28 Jun 2021, 19:13 last edited by
Have you looked at property alias?
MyItem.qml:
Item { property alias text: subText.text Text { id: subtext } }
TopLevel.qml:
Item { MyItem { id: myItem } Button { onClicked: myItem.text = "clicked" } }
-
wrote on 29 Jun 2021, 17:54 last edited by
Thanks for your answer ^^
I totally forgot the existance of alias ! Thank you for the remind ^^
1/3