[Solved] Cannot resolve local file url

  • I'm trying to load an external QML file dynamically. The following code works when I use a FileDialog to resolve the path:

    var Url = fileDialog.fileUrl;
    console.log(Url.toString()); // qml: file:///C:/Templates/benchmark.qml
    templateLoader.source = Url;

    But when I try to assign the source using a string, I get a "File not found" error:

    var sUrl = "file:///C:/Templates/benchmark.qml";
    templateLoader.source = sUrl;

    This seems to be related to the problem in this discussion: http://qt-project.org/forums/viewthread/19528 but when I try the binding trick it doesn't work. What am I doing wrong?

  • Try using Qt.resolvedUrl(absolute file name)

  • I had tried that, but it didn't help:

    var sFilePath = "C:/Templates/benchmark.qml";
    var sUrl = "file:///C:/Templates/benchmark.qml";
    templateLoader.source = Qt.resolvedUrl(sFilePath); // Network error
    templateLoader.source = Qt.resolvedUrl(sUrl); // File not found

  • I'm surprised. Just see my example here. It works perfectly here.

    @Rectangle {
    width: 240
    height: 280;color :"blue"
    Loader {
    id : load
    anchors.fill: parent
    onClicked: {
    load.source = Qt.resolvedUrl("/Users/dheeru/Qt/Training/QML/QtQuickTraining/DynamicObjects/UseLoader/LoaderWithAbosolutePath/TestAbsPath.qml")


    @import QtQuick 2.0

    Rectangle {
    width: 100
    height: 200
    color : "yellow"

  • Dumb. Dumb. Dumb. I found a typo in my hard-coded path. It works fine when I actually look at my code.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.