Reputation: 19800
I'm trying to set a property of a custom QML element conditionally. Meaning I only want to set it if something is true.
In this example I want to set someProperty
when the application has a specific value, but if not, I want to let the object retain the original value. Also I want to let it behave like a binding. Meaning if the condition changes, it restores/sets the property again.
main.qml
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MyObject {
someProperty: (title == "different" ? "other_value" : 'undefined')
}
}
MyObject.qml
Rectangle {
property var someProperty: "original_value"
Component.onCompleted: {
console.error("MyOject.someProperty: " + someProperty)
}
}
So this prints:
qml: MyOject.someProperty: undefined
I want it to print
qml: MyOject.someProperty: original_value
So I have tried:
someProperty: (title == "different" ? "other_value")
: errorsomeProperty: (title == "different" ? "other_value" : someProperty)
: binding loopI don't want to use any onCompleted
kind of syntax because I want to retain the binding and not litter my code.
Upvotes: 1
Views: 2119
Reputation: 13711
You might use States
together with PropertyChanges
and set the State to be active, when the condition is met.
The State
-System has the property that it saves restores the original state, once a State
is left again.
Upvotes: 4
Reputation: 5207
You could try with a Binding
element
MyObject {
id: myObject
}
Binding {
target: myObject
property: "someProperty"
value: "other_value"
when: title == "different"
}
Upvotes: 1