Please nominate your Qt Champions for 2021!

[SOLVED] Use relative path of qml file A in qml file B

  • I have the two qml files A and B. Both are in different directories.
    A is used in B and should display an image, which is located in the directory of B.
    But if I set the source of the image in A to "imageName.png" it doesn't work, since it uses the relative path of A and not of B.
    So how can I use the relative path of B in A?

    Here is an example:

    Rectangle {
    property string imageSource
    Image { source: imageSource }

    Rectangle {
    A {
    imageSource: "image.png"

    Image is located at /other/Path/to/image.png (which is the path of B.qml)

    The error message is like: Cannot find /path/to/image.png, which shows that the relative path of A.qml is used and not of B.qml

    So how can I use the relative path of B.qml in order to set the right path to the image for A.qml?

  • Moderators


    I think you can use Qt."resolvedUrl(url url)": here which will give path of image.png relative to B.

  • Works perfect, thanks :)

Log in to reply