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. QML scope: how to access objects?
Forum Update on Monday, May 27th 2025

QML scope: how to access objects?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 1.6k 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.
  • E Offline
    E Offline
    Eligijus
    wrote on 17 Aug 2017, 06:55 last edited by
    #1

    Hi,

    I have 2 files

    //Cat.qml
    
    Item {
        Rectangle {
            id: testRect
            width: 50
            height: 50
            border.color: "black"
    
            function printTest(txt) {
                console.log(txt)
            }
        }
    
        Button {
            anchors.left: testRect.right
            width: 50
            height: 50
            onClicked: {
                testRect.printTest("button click")
            }
        }
    
    }
    
    //main.qml
    Window {
        visible: true
        width: 640
        height: 480
    
        Cat {
            id: testCat
        }
    
        Button {
            x: 100
            onClicked: {
    
            }
        }
    }
    

    How to access Rectangle in Cat.qml?
    More specifically I want to printTest in main.qml onClicked

    R E 2 Replies Last reply 17 Aug 2017, 07:24
    0
    • E Eligijus
      17 Aug 2017, 06:55

      Hi,

      I have 2 files

      //Cat.qml
      
      Item {
          Rectangle {
              id: testRect
              width: 50
              height: 50
              border.color: "black"
      
              function printTest(txt) {
                  console.log(txt)
              }
          }
      
          Button {
              anchors.left: testRect.right
              width: 50
              height: 50
              onClicked: {
                  testRect.printTest("button click")
              }
          }
      
      }
      
      //main.qml
      Window {
          visible: true
          width: 640
          height: 480
      
          Cat {
              id: testCat
          }
      
          Button {
              x: 100
              onClicked: {
      
              }
          }
      }
      

      How to access Rectangle in Cat.qml?
      More specifically I want to printTest in main.qml onClicked

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 17 Aug 2017, 07:24 last edited by
      #2

      @Eligijus
      you can expose it as a property for example:

      // Cat.qml
      
      Item {
          property alias embeddedRect: testRect
      
          Rectangle {
              id: testRect
              ....
          }
      
         ...
      }
      
      //main.qml
      
      Window {
          
          // use 'testCat.embeddedRect'
      
          Cat {
              id: testCat
          }
      
          ....
      }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • E Eligijus
        17 Aug 2017, 06:55

        Hi,

        I have 2 files

        //Cat.qml
        
        Item {
            Rectangle {
                id: testRect
                width: 50
                height: 50
                border.color: "black"
        
                function printTest(txt) {
                    console.log(txt)
                }
            }
        
            Button {
                anchors.left: testRect.right
                width: 50
                height: 50
                onClicked: {
                    testRect.printTest("button click")
                }
            }
        
        }
        
        //main.qml
        Window {
            visible: true
            width: 640
            height: 480
        
            Cat {
                id: testCat
            }
        
            Button {
                x: 100
                onClicked: {
        
                }
            }
        }
        

        How to access Rectangle in Cat.qml?
        More specifically I want to printTest in main.qml onClicked

        E Offline
        E Offline
        Eeli K
        wrote on 17 Aug 2017, 08:04 last edited by
        #3

        @Eligijus On the other hand you don't need to place printTest inside testRect, you can place it (or any other function) directly inside the top level item because you have access to all ids and properties inside the same file. Especially in your simplified example the function could be as well global or located anywhere because it's completely self-contained and uses only the argument.

        Item {
            //can be called in your main.qml as testCat.printTest("works well"):
            function printTest(txt) {
                    console.log(txt)
                    console.log(testRect.width)
            }
            Rectangle {
                id: testRect
                width: 50
                height: 50
                border.color: "black"
            }
        }
        
        E 1 Reply Last reply 17 Aug 2017, 08:24
        1
        • E Eeli K
          17 Aug 2017, 08:04

          @Eligijus On the other hand you don't need to place printTest inside testRect, you can place it (or any other function) directly inside the top level item because you have access to all ids and properties inside the same file. Especially in your simplified example the function could be as well global or located anywhere because it's completely self-contained and uses only the argument.

          Item {
              //can be called in your main.qml as testCat.printTest("works well"):
              function printTest(txt) {
                      console.log(txt)
                      console.log(testRect.width)
              }
              Rectangle {
                  id: testRect
                  width: 50
                  height: 50
                  border.color: "black"
              }
          }
          
          E Offline
          E Offline
          Eligijus
          wrote on 17 Aug 2017, 08:24 last edited by
          #4

          @Eeli-K In that particular example yes that would work. But I forgot to mention that I provided simplified example. In my case Rectange(testRect) is C++ object and printTest is a public slot. So I can't simply move that slot to top level item.

          E 1 Reply Last reply 17 Aug 2017, 10:57
          0
          • E Eligijus
            17 Aug 2017, 08:24

            @Eeli-K In that particular example yes that would work. But I forgot to mention that I provided simplified example. In my case Rectange(testRect) is C++ object and printTest is a public slot. So I can't simply move that slot to top level item.

            E Offline
            E Offline
            Eeli K
            wrote on 17 Aug 2017, 10:57 last edited by
            #5

            @Eligijus OK. Raven-worx's solution may be the best for you, it's simple. Another option is a function which delegates the call:

            Item {
                //can be called in your main.qml as testCat.printTest("works well"):
                function printTest(txt) {
                       testItem.printTest(txt)
                }
                MyItemWithASlot {
                    id: testItem
                }
            }
            
            E 1 Reply Last reply 17 Aug 2017, 11:10
            0
            • E Eeli K
              17 Aug 2017, 10:57

              @Eligijus OK. Raven-worx's solution may be the best for you, it's simple. Another option is a function which delegates the call:

              Item {
                  //can be called in your main.qml as testCat.printTest("works well"):
                  function printTest(txt) {
                         testItem.printTest(txt)
                  }
                  MyItemWithASlot {
                      id: testItem
                  }
              }
              
              E Offline
              E Offline
              Eligijus
              wrote on 17 Aug 2017, 11:10 last edited by
              #6

              @Eeli-K Yeah i'm sticking with the Raven-worx's solution it's elegant and simple. Thanks for all your input.

              1 Reply Last reply
              0

              1/6

              17 Aug 2017, 06:55

              • Login

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