[SOLVED] QML Loader: load once, use many



  • I have a cpp object that has a visible flag. I also have a QML loader that loads the QML file when the cpp object visible flag is true:

    @
    Loader {
    source: myCppObj.visible ? "BigWindow.qml" : ""
    // Do some cpp/qml bindings
    }
    @

    That is fine, but I want to keep the BigWindow in memory when it was visible once. So I don't want to set the source to "" when myCppObj.visible is false...

    Anybody knows a nice tric to set the source once (when the myCppObj.visible flag is true for the first time) and keep the source untouched when the myCppObj.visible flag is changed?



  • I found a trick:

    @
    Loader {
    id: myLoader
    Binding { target: myLoader; property: "source"; value: "BigWindow.qml"; when: myCppObj.Visible == true}
    }
    @
    This trick will set the source property of loader when myCppObj.visible is true. When it is set to false the source stays the same. Also the loader doesn't reload the BigWindow.qml when it is already set.

    The problem is that the loader cannot find BigWindow.qml anymore. Only when i set the absolute path (for example: /opt/myapp/qml/windows/BigWindow.qml") the loader loads the qml file. But I don't want to use the absolute path, I want to use the relative path (starting from qml directory, like the source property of Loader normally uses)!

    The use of normal source property works, it automatically uses the qml directory :
    @
    Loader
    source: "BigWindow.qml"
    @

    I also tried to set the sourceComponent property of Loader but then I don't see BigWindow and also no error is printed that it cannot find the component/file...



  • It seems like something is going wrong with URL resolution. If you could create a bug report with the details at http://bugreports.qt-project.org , that would be very helpful. You may be able to work around the issue with something like:

    @
    Loader {
    id: myLoader
    property url bigWindowUrl: "BigWindow.qml"
    Binding {
    target: myLoader
    property: "source"
    value: bigWindowUrl
    when: myCppObj.Visible == true
    }
    }
    @

    (this should force earlier resolution)

    Regards,
    Michael



  • Thanks, this works!!!


Log in to reply
 

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