5

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?

4
  • 1
    I can't test it right now but isn't there a isEmpty or empty property like QUrl::isEmpty()? Sadly the QML doc is not so generous when it comes to basic QML types.
    – xander
    Commented Jan 5, 2018 at 10:15
  • @xander You mean something like typeof selectedImage === "undefined"? I have not tried it in this case. I'm also not aware of something like isEmpty as I cannot find information about it (yet).
    – Matteo
    Commented Jan 5, 2018 at 10:44
  • No I just mean as a bool property of url, like selectedImage.isEmpty or selectedImage.empty, can you try to log that? See the Qt doc of QUrl I've linked above, but as mentioned there is no documentation for QML url, at least not about properties/functions in any way. Since your selectedImage is of type url and not a simple string.
    – xander
    Commented Jan 5, 2018 at 10:52
  • Sadly they both return undefined, I've updated the question with this new information.
    – Matteo
    Commented Jan 5, 2018 at 10:57

1 Answer 1

5

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.

5
  • How this is better than strict checking for ""?
    – Matteo
    Commented Jan 8, 2018 at 11:17
  • I read your question more carefully and noticed that you were actually asking more specifically. I don't think it was a good idea to provide example code which was already faulty and then wonder why it's not working "If I try to do something..." Anyway, I edited my answer and updated more specific answer too.
    – talamaki
    Commented Jan 8, 2018 at 12:23
  • So this is the only way to do it?
    – Matteo
    Commented Jan 8, 2018 at 12:56
  • 1
    I would say so. The url QML basic type doesn't provide any function for such checking so converting it to a string seems to be the only option at the moment. In the comment field of the open bug "Return local file path from url QML type" (bugreports.qt.io/browse/QTBUG-54988) you can see suggestion of "Url" type that provides similar API to QUrl, whilst the "url" value type stays as is. QUrl::isEmpty() would be the function in the C++ side. However, the status of the bug is unresolved.
    – talamaki
    Commented Jan 8, 2018 at 14:50
  • Oh boy, I feel so silly. I've trying to so a basic string comparison on a url for 30 minutes. Where in the fine documentation did you find toString()? I can only see URL(), which converts to a Javascript URL, which has a toString(). But it turns out toString() can be called directly and all is well. Commented Mar 21, 2022 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.