6
votes

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 ?
1
I've had a similar question to ask. Can you edit your question in order to get an explanation about why it's not possible to access the value of the id of an object ?SR_

1 Answers

2
votes

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