Traversing like DOM



  • Hi,

    First sorry for my english... i'm french and not very good in english.
    I start learning QML and have a simple question :

    Imagine a code like this :

    @import QtQuick 2.2
    import QtQuick.Window 2.1

    Window {
    id: mywindow

    Rectangle{
        id: rect2
        x: 0
        y: 0
    }
    
    Rectangle{
        id: rect1
        x: 10
        y: 10
    }
    
    Rectangle{
        id: rect3
        x: 20
        y: 20
    }
    

    }
    @

    What i want, is make a function in qml (no c++ via qt) to traverse my window and get all the elements rectangle to get theire x and y positions.

    Is it possible ?

    Thanks

    Stéphane


  • Moderators

    Hi,

    Welcome to Qt Devnet.
    You can use "data":http://qt-project.org/doc/qt-5/qml-qtquick-window-window.html#data-prop property to get list of visual childrens and then iterate over the list. Put this logic in a function and call it on Component.onCompleted. Something like
    @
    function getChilds() {
    for(var a=0;a<mywindow.data.length;a++){
    console.log(mywindow.data[a].x)
    }
    }
    @

    will print all x coordinates of the visual items.

    P.S: Your english perfect and completely understandable :)



  • Hi and thanks....

    But now a small complement to my first question....

    Is there a solution to retrieve all the elements of a type like this in js/jquery :

    $(".myclass").each(...

    I understand the possibility with your answer but maybe there is a small hack to be more easy than scan everything just for extract an objectname (for example)

    Stéphane


  • Moderators

    Well I'm not aware of any such inbuilt function but I guess that jquery should be doing the same internally.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.