Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to create global id which is visible on the other qml files?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 1.8k Views
  • 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 Offline
    P Offline
    Pyroxar
    wrote on last edited by
    #1

    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
    0
    • P Offline
      P Offline
      Pyroxar
      wrote on last edited by Pyroxar
      #4

      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
      0
      • F Offline
        F Offline
        Fheanor
        wrote on last edited by Fheanor
        #2

        Is something like that what you want ?

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

        loaderExample.qml

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thesourcehim
          wrote on last edited by
          #3

          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
          1
          • P Offline
            P Offline
            Pyroxar
            wrote on last edited by Pyroxar
            #4

            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
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved