I've a simple component like this one:
SimpleComponent.qml
Image {
id: root
property url selectedImage: ""
property bool selected: false
states: State {
name: 'selected'
when: selectedImage !== "" && selected
PropertyChanges { target: root; source: selectedImage; }
}
}
If I try to do something like the following, the image source will be replaced by selectedImage even if the condition should not be true.
SimpleImplementation.qml
Item {
id: root
SimpleComponent {
id: simpleSwitchImage
source: "/path/to/image.png"
selected: true
}
}
Attaching the following to Component.onCompleted I've got the commented results:
console.log(
selectedImage, // empty string
selectedImage === "", // false
selectedImage === undefined, // false
selectedImage === null, // false
selectedImage === Qt.resolvedUrl(""), // false
selectedImage.toString(), // empty string
selectedImage.toString() === "", // true
selectedImage.isEmpty, // undefined
selectedImage.empty // undefined
)
According to the documentation the only true I've got is the absolute path to the resource, is this the correct way to do such a simple check for an empty property of type url?
In the SimpleComponent.qml example code you are using strict inequality comparison between url QML type and zero length JavaScript string which is wrong:
when: selectedImage !== "" && selected
You could use url QML type's toString() method to get the url contents as a string
when: selectedImage.toString() !== "" && selected
or use JavaScript string's length property for checking:
when: selectedImage.toString().length>0 && selected
More specific answer: I don't think there is any better way to check the "emptyness" of the url QML type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With