Qt World Summit: Submit your Presentation

[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:
    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?


  • Okay, "Qt.resolvedUrl()" ... pretty simple :)
    All that time reading about import paths and it was right here all along

Log in to reply