Search code examples
c++qtqmlqt-quickresource-file

File inside resource cannot find local file


I have a project with several QML files. All of them are inside resource file. Now I want to load external image, from local file system. But I cannot find a way to do that.

Image {
    source: "images/image.png" // that tries to load file as 'qrc:/images/image.png'
    source: "file://images/image.png" // that does not work (file not found)
}

So now I'm a bit confused, how can I load the file in right way?


Solution

  • The QML engine assumes that relative paths addressed in QML files stored in the Qt Resource System are resolved within that resource file. So if your QML file is in a resource and you want to access a file in the application directory path you should set the path from c++ :

    engine.rootContext()->setContextProperty("applicationPath", "file://"+qApp->applicationDirPath()+ "/");
    

    Now you can address the file in QML :

    Image {
        source: applicationPath + "images/image.png"
    }