How to get id of child in qml

user2614242

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 ?
Zen

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the ID of a child when mouseover parent

From Dev

How to get the ID of a child when mouseover parent

From Dev

How can I get the id of a child element/

From Dev

How to get a nested list of a child by Parent id?

From Dev

Get unique device ID in QML

From Dev

Get child id onclick

From Dev

How to get the parent div id from the anchor child

From Dev

How to get all descendent child process id of pid in c in linux

From Dev

How can I get a definite child of div without class/id

From Dev

How to get the id of a very short child process if the parent is known?

From Dev

How to get all child of a given id in SQL Server query

From Dev

Rails 4.0 - How to get collection of objects based on an ID of a child object?

From Dev

How to get child table rows using parent table ID?

From Dev

Nodejs how to get child process from process Id

From Dev

Get or create child actor by ID

From Dev

Get or create child actor by ID

From Dev

QML how to get the position of Loader

From Dev

Is it possible to get any child component under mouseX,mouseY in QML

From Dev

How to get ID Parent from Child ID when i have className of the Parents JQuery/JS

From Dev

how to get prev and child

From Dev

How to get child - Firebase

From Dev

How to find QML item by its string id?

From Dev

How to connect a qml child component signal to a c++ slot

From Dev

Alfresco: how get child of a child with javascript (and FTL)

From Dev

How to get a child of child ? Umbraco CMS

From Dev

How to access child properties of MainForm.ui.qml from main.qml in Qt 5.5?

From Dev

Supposed to get the child but somehow gets the parent ID

From Dev

Get parents based on child id SQL

From Dev

get child id from parent javascript onclick

Related Related

  1. 1

    How to get the ID of a child when mouseover parent

  2. 2

    How to get the ID of a child when mouseover parent

  3. 3

    How can I get the id of a child element/

  4. 4

    How to get a nested list of a child by Parent id?

  5. 5

    Get unique device ID in QML

  6. 6

    Get child id onclick

  7. 7

    How to get the parent div id from the anchor child

  8. 8

    How to get all descendent child process id of pid in c in linux

  9. 9

    How can I get a definite child of div without class/id

  10. 10

    How to get the id of a very short child process if the parent is known?

  11. 11

    How to get all child of a given id in SQL Server query

  12. 12

    Rails 4.0 - How to get collection of objects based on an ID of a child object?

  13. 13

    How to get child table rows using parent table ID?

  14. 14

    Nodejs how to get child process from process Id

  15. 15

    Get or create child actor by ID

  16. 16

    Get or create child actor by ID

  17. 17

    QML how to get the position of Loader

  18. 18

    Is it possible to get any child component under mouseX,mouseY in QML

  19. 19

    How to get ID Parent from Child ID when i have className of the Parents JQuery/JS

  20. 20

    how to get prev and child

  21. 21

    How to get child - Firebase

  22. 22

    How to find QML item by its string id?

  23. 23

    How to connect a qml child component signal to a c++ slot

  24. 24

    Alfresco: how get child of a child with javascript (and FTL)

  25. 25

    How to get a child of child ? Umbraco CMS

  26. 26

    How to access child properties of MainForm.ui.qml from main.qml in Qt 5.5?

  27. 27

    Supposed to get the child but somehow gets the parent ID

  28. 28

    Get parents based on child id SQL

  29. 29

    get child id from parent javascript onclick

HotTag

Archive