Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to check url type value?

Tags:

qt

qt5

qml

qtquick2

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?

like image 408
Matteo Avatar asked Jan 05 '18 08:01

Matteo


1 Answers

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.

like image 108
talamaki Avatar answered Sep 16 '22 16:09

talamaki