Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Traversing like DOM
Qt 6.11 is out! See what's new in the release blog

Traversing like DOM

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 2.2k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    csgib
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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 :)

      157

      1 Reply Last reply
      0
      • C Offline
        C Offline
        csgib
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

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

          157

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