How to get id of child in qml

11,775

Id is not an ordinary object property, so it is undefined when you try to assess it through js. And qml doesn't provide operators like typeof. So you need to add type or objectname property manually. I would consider subclassing Image and adding type. Ref: How to do a "is_a", "typeof" or instanceof in QML?

Item 
{
id: root

Image {
    id: background
    type: "image"
}

Image {
    id: pole
    type: "image"
}
function iter(){
     for(var i = 0; i < root.children.length; ++i)
         if(root.children[i].type==="image"){
         //balabala
         }
     }
}
}
Share:
11,775
user2614242
Author by

user2614242

Updated on June 08, 2022

Comments

  • user2614242
    user2614242 almost 2 years

    When I tried to get children ID's slightly modifying this http://qmlbook.github.io/en/ch01/index.html example

    // animate.qml
    
    import QtQuick 2.0
    
    Item 
    {
        id: root
        width: background.width
        height: background.height
        property string information: "this is root"
    
        property int rotationStep: 45
    
        Image {
            id: background
            source: "images/background.png"
        }
    
        Image {
            id: pole
            property string information: "this is pole"
            x: (root.width - width)/2+2
            y: root.height - height
            source: "images/pole.png"
        }
    
        Image {
            id: pinwheel
            anchors.centerIn: parent
            source: "images/pinwheel.png"
            property string information: "this is pinweel"
            // visible: false
            Behavior on rotation {
                NumberAnimation { duration: 125 }
            }
        }
    
        Image {
            id: blur
            opacity: 0
            anchors.centerIn: parent
            source: "images/blur.png"
             property string information: "this is blur"
            // visible: false
            Behavior on rotation {
                NumberAnimation { duration: 125 }
            }
            Behavior on opacity {
                NumberAnimation { duration: 125 }
            }
    
        }
    
        // M1>>
        focus: true
        Keys.onLeftPressed: {
            blur.opacity = 0.8
            pinwheel.rotation -= root.rotationStep
            blur.rotation -= root.rotationStep
        }
        Keys.onRightPressed: {
            blur.opacity = 0.5
            pinwheel.rotation += root.rotationStep
            blur.rotation += root.rotationStep
        }
        Keys.onReleased: {
            blur.opacity = 0
        }
    
        Keys.onSpacePressed:
        {
            for (var i=0; i<root.children.length; ++i)
                console.info(root.children[i].information)
        }
    
        Keys.onDeletePressed:
        {
            for (var i=0; i<root.children.length; ++i)
                console.info(root.children[i].id)
        }
    
    
        // <<M1
    }
    

    Unfortunately pressing Delete key gives me an error:

    qml: undefined
    qml: undefined
    qml: undefined
    qml: undefined
    

    as opposed to pressing spacebar:

    qml: undefined
    qml: this is pole
    qml: this is pinweel
    qml: this is blur
    

    Why this script returns undefined id's ?

    I need to traverse some objects and be able to tell what is what - so I need to know how to traverse root tree to get id's of its childs and their object types.

    Unfortunately I was unable to print the most trival id's and had to add some simple property to get it done but this means a lot of work in real life project since every object needs information property :(

    So to reiterate:

    1. Why the id's in this example are undefined?
    2. How to traverse object tree using qml and print its id's and types ?
  • user2614242
    user2614242 over 8 years
    But why id is not provided? I can understand why it should be read-only , but what is the reason in terms of application engineering not to provide id for developer?
  • Admin
    Admin over 7 years
    @user2614242 The id is not a real property. From the docs: "While it may look like an ordinary property, the id attribute is not an ordinary property attribute, and special semantics apply to it; for example, it is not possible to access myTextInput.id in the above example."