Get the original string source in a QML file from an url property
-
My approach would be to not use the source property of the Image element directly. Instead, you create a new property in your own item, that you make persistent and that you initialize to your testImage.png by default. Then, you bind the Image::source property to this custom property.
-
Thanks Andre!
However, then all users would need to replace the Image element with a custom overwritten Image element that adds this string property - can you think of another way to achieve the same result without changing APIs or introducing new string properties for every url property I would like to store?
-
Users are programmers using our APIs, and since we use the normal Image element for non-animated images, it would be tedious for them to change the source property to any other non-familiar one.
However, the question is not specific to our APIs, but rather a general QUrl-related one I guess.
-
-
Hi Thomas,
I did check the QUrl APIs, but this is rather a QML- & QUrl-related issue. The QUrl API is good, but for the use-case as shown above (extract the string part the developer entered in the QML file) it does not provide a solution. That's why I asked the question here :)Cheers,
Chris -
Nope, as I have written above:
bq. In the above case one would assume that the solution is simple, because you might just cut everything except the file name and ending. But what if the image source contains a relative path, e.g. “img/anotherImage.png” or “../../otherFolder/image.png” – in that case I would like to get exactly these strings and not the full path.
-
Sorry for being slow, but as far as I know things like "../foo" do not get resolved, they show up in the url as such. Do you mean you see them resolved?
Maybe you mean you want to remove the prefix, the basepath. For that see the declaritive engine, it has a getter for that.
-
Hi,
we fixed this issue by querying the baseUrl of the context for the object with @QDeclarativeEngine::contextForObject()@ (which returns the qml file where the QObject resides) and then resolving the path to it relative from there. The disadvantage is, that you need to provide the pointer to the root element in your qml file to a C++ function, but that is a valid tradeoff. Maybe that helps someone. Thanks for your hints!Cheers,
Chris