Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    QML and Qt Quick
    3
    4
    1450
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      Pyroxar last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • P
        Pyroxar last edited by Pyroxar

        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});
        }
        
        1 Reply Last reply Reply Quote 0
        • F
          Fheanor last edited by Fheanor

          Is something like that what you want ?

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

          loaderExample.qml

          1 Reply Last reply Reply Quote 0
          • T
            thesourcehim last edited by

            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});

            1 Reply Last reply Reply Quote 1
            • P
              Pyroxar last edited by Pyroxar

              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});
              }
              
              1 Reply Last reply Reply Quote 0
              • First post
                Last post