[solved] Relative URL when creating a reusable QML PageLoader component



  • Hiya,

    I'm trying to create a PageLoader component for QML. So say I defined a PageLoader.qml file and it has something like the following in it:
    @
    Item
    {
    property var pageStack: [];
    function push(page_url) {}
    function pop() {}

    Loader {
    // do stuff here later
    }
    }
    @

    PageLoader is located along side a bunch of reusable components in /stuff/UIComponents/. A project that's going to use UIComponents is located in /stuff/project/main.qml. In main.qml I call import:

    @
    import QtQuick 2.0
    import "../UIComponents"

    Rectangle {
    PageLoader { id:pageloader; }
    Button { onClicked: pageloader.push("hello.qml"); }
    }
    @

    And this is where I run into trouble. "hello.qml" is intended to be specified relative to "main.qml", because that's where PageLoader has been instantiated. However, PageLoader saves "hello.qml" into its pageStack property and then changes the source of Loader whenever its ready. Loader treats this as a path relative to the component location (in /stuff/uicomponents) and I get an error like:
    @
    /stuff/uicomponents/hello.qml: File not found
    @

    So what am I supposed to do here? How can I pass the right path to my custom component (/stuff/projects/hello.qml) if I want to specify it as a relative path?

    Preet



  • Okay, "Qt.resolvedUrl()" ... pretty simple :)
    All that time reading about import paths and it was right here all along
    http://qt-project.org/doc/qt-5.0/qtqml/qml-url.html


Log in to reply
 

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