Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Acessing childrens by ID?
Qt 6.12 LTS is out! See what's new in the release blog

Acessing childrens by ID?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 4 Posters 1.4k 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.
  • S Offline
    S Offline
    shokarta
    wrote on last edited by
    #1

    Hello as... as I have complex:

    main.qml:

    ApplicationWindow {
        id: mainWindow
    
        // here is where i need to get the TextFiled text set as "something"
    
        // Views (initialy used SwipeView)
        StackLayout {
            id: view
            anchors.fill: parent
            currentIndex: 0
    
            Item {
                id: view_InitialScreen
                InitialScreen {}
            }
            Item {
                id: view_ChoiseOffline
                ChoiseOffline {}
            }
        }
    }
    

    ChooseOffline.qml:

    Rectangle {
        id: parentObject
        anchors.fill: parent
    
        Rectangle {
            id: testRect
    
            Rectangle {
                id: preselectedStrloRectangle
    
                Rectangle {
                    id: scanRectangle
    
                    Image {
                        id: scanImage
                    }
                }
    
                Rectangle {
                    id: preselectedStrloRectangle1
    
                    Label {
                        id: preselectedStrloLabel1
                    }
                }
                Rectangle {
                    id: preselectedStrloRectangle2
    
                    TextField {
                        id: preselectedStrloTextfield2
                        text: "something"
                    }
                }
                Rectangle {
                    id: preselectedStrloRectangle3
    
                    Image {
                        id: clearImage
                    }
                }
            }
    
            Rectangle {
                id: serialNumberRectangle
    
                Rectangle {
                    id: serialNumberRectangle1
    
                    Label {
                        id: serialNumberLabel1
                    }
                }
                Rectangle {
                    id: serialNumberRectangle2
    
                    TextField {
                        id: serialNumberTextfield2
                    }
                }
            }
    
    	... and so on
    }
    

    so far the only way I was able to console.log() the text value is by:

    console.log(view_ChoiseOffline.children[0].children[0].children[0].children[2].children[0].text)
    

    but this is a terrible solution as Im keep adjuting the code, adding more childrens above and bellow so this would constantly change...

    everytime i try to use IDs instead of children[int] i get an error that Cannot read property xxxx of undefined.
    i have tried to use difenret id name instead of children[0], or simply children[parentObject]/children['parentObject'] or ucing "item" instead of "children".

    so is there any way to access it simply by ID(s) out of the main.qml?

    1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      set objectName to the children. Then something like this?

           for(var i = 0; i < view_ChoiceOffline.children.length; ++i)
               if(view_ChoiceOffline.children[i].objectName==="something"){
                    console.log(view_ChoiceOffline.children[i].text)
               }
           }
      
      1 Reply Last reply
      0
      • Marko StankeM Offline
        Marko StankeM Offline
        Marko Stanke
        wrote on last edited by
        #3

        What you could do is inside of your ChooseOffline.qml and inside of parentObject, define an alias for the text you want to access:

        property alias preselectedTextfield: preselectedStrloTextfield2

        and then from main.qml you can access it with view_ChoiseOffline.preselectedTextfield.text

        S 1 Reply Last reply
        1
        • Marko StankeM Marko Stanke

          What you could do is inside of your ChooseOffline.qml and inside of parentObject, define an alias for the text you want to access:

          property alias preselectedTextfield: preselectedStrloTextfield2

          and then from main.qml you can access it with view_ChoiseOffline.preselectedTextfield.text

          S Offline
          S Offline
          shokarta
          wrote on last edited by
          #4

          @Marko-Stanke said in Acessing childrens by ID?:

          What you could do is inside of your ChooseOffline.qml and inside of parentObject, define an alias for the text you want to access:

          property alias preselectedTextfield: preselectedStrloTextfield2

          and then from main.qml you can access it with view_ChoiseOffline.preselectedTextfield.text

          well this does not work... however what I have managed to make it work is to set the alias inside parentObject:

          property alias aliasPreselectedLocation: preselectedStrloTextfield2
          

          which Im able to call from mainWindow via:

          view_ChoiseOffline.children[0].aliasPreselectedLocation.text
          

          definitely not nice way, but at least it works...

          but back to my question... can I use children with string value (of ID idealy) instead of integer counter?
          something like:

          children['parentObject']
          

          I tried several ways but i have never made it work

          1 Reply Last reply
          0
          • J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            how about:

            main.qml:

            ApplicationWindow {
                id: mainWindow
            
                // here is where i need to get the TextFiled text set as "something"
            
                // Views (initialy used SwipeView)
                StackLayout {
                    id: view
                    anchors.fill: parent
                    currentIndex: 0
            
                    Item {
                        id: view_InitialScreen
                        InitialScreen {}
                    }
                    Item {
                        id: view_ChoiseOffline
                        ChoiseOffline {id:chOffline;}
                    }
                }
            }
            

            ChooseOffline.qml:

            Rectangle {
                id: parentObject
                anchors.fill: parent
               property alias aliasPreselectedLocation: preselectedStrloTextfield2
            ....
            }
            
            console.log(chOffline.aliasPreselectedLocation.text)
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            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
            • Unsolved