How to create global id which is visible on the other qml files?



  • I have Loader object which has id "pageLoader".
    I load first "first.qml" and i need to change the page to other with the position first page but "pageLoader" is not visible.
    What can i do that "pageLoader" make globally?



  • Is something like that what you want ?

    Component {
       id: pageLoader
       Loader {
           ...
       }
    }
    

    loaderExample.qml



  • Add a property "parentLoader" (for example) to first.qml, then when using Loader.setSource pass your loader (by it's id) to "parentLoader" via second parameter of setSource. In first.qml address the loader via parentLoader property instead of id.

    pageLoader.setSource("first.qml", {"parentLoader": pageLoader});



  • Thank you!

    In main.qml file

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.0
    import QtQuick.Window 2.2
    import QtQuick.Layouts 1.3
    
    ApplicationWindow {
        visible: true
        width: Screen.width
        height: Screen.height
        title: qsTr("Hello World")
    
        property Loader parentLoader: pageLoader
        Loader { id: pageLoader; anchors.fill: parent; source: "Hello.qml" }
    }
    

    and anywhere else:

    Button {
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.bottom: parent.bottom
                    anchors.bottomMargin: 30
                    width: 150
                    text: "CLICK ME"
                    onClicked: pageLoader.setSource("MyQMLFile.qml", {"parentLoader": pageLoader});
    }
    

Log in to reply