Setter function for internal qml object
Solved
QML and Qt Quick
-
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 ?
-
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" } }