Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Iterate over top two levels of Treeview nodes?
Qt 6.11 is out! See what's new in the release blog

Iterate over top two levels of Treeview nodes?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 705 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.
  • T Offline
    T Offline
    Taytoo
    wrote on last edited by
    #1

    Have a Treeview with 3 levels, is there a simple way to iterate in a single loop over the top two levels:

    • Node 1
      -> Node 2
      --> Node 3
    • Node 4
    • Node 5
      -> Node 6
      --> Node 7

    Need to loop over Nodes: 1, 2, 4, 5, 6

    JonBJ 1 Reply Last reply
    0
    • T Taytoo

      Have a Treeview with 3 levels, is there a simple way to iterate in a single loop over the top two levels:

      • Node 1
        -> Node 2
        --> Node 3
      • Node 4
      • Node 5
        -> Node 6
        --> Node 7

      Need to loop over Nodes: 1, 2, 4, 5, 6

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Taytoo

      for node0 in topLevelNodes:
          doSomething(node0)
          for node1 in node0.children:
              doSomething(node1)
      
      T 1 Reply Last reply
      0
      • JonBJ JonB

        @Taytoo

        for node0 in topLevelNodes:
            doSomething(node0)
            for node1 in node0.children:
                doSomething(node1)
        
        T Offline
        T Offline
        Taytoo
        wrote on last edited by
        #3

        @JonB That's what I'm doing currently. Was hoping there was a better way to get nodes of n-levels as a singular list and iterate over them.

        kshegunovK JonBJ 2 Replies Last reply
        0
        • T Taytoo

          @JonB That's what I'm doing currently. Was hoping there was a better way to get nodes of n-levels as a singular list and iterate over them.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @Taytoo said in Iterate over top two levels of Treeview nodes?:

          @JonB That's what I'm doing currently. Was hoping there was a better way to get nodes of n-levels as a singular list and iterate over them.

          It is possible by using a different representation of the tree. It is not immediately straightforward though. Typically trees can be 'linearized' by keeping a left:right traversal index (the so-called "nested set model").

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1
          • T Taytoo

            @JonB That's what I'm doing currently. Was hoping there was a better way to get nodes of n-levels as a singular list and iterate over them.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Taytoo said in Iterate over top two levels of Treeview nodes?:

            Was hoping there was a better way to get nodes of n-levels as a singular list and iterate over them.

            There is, but that is not what you asked for. You wrote:

            is there a simple way to iterate in a single loop over the top two levels

            My answer would not scale for n-levels. Nor would it be right for "visit every node which is not a leaf". One would probably do either of those recursively. And it would not be a "single loop", which I now notice but did not before, if that is what is vital to you. So you have to state exactly what it is you are looking for.

            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