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?
QtWS25 Last Chance

Iterate over top two levels of Treeview nodes?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 513 Views
  • 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 12 Mar 2021, 21:03 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

    J 1 Reply Last reply 12 Mar 2021, 22:15
    0
    • T Taytoo
      12 Mar 2021, 21:03

      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

      J Offline
      J Offline
      JonB
      wrote on 12 Mar 2021, 22:15 last edited by
      #2

      @Taytoo

      for node0 in topLevelNodes:
          doSomething(node0)
          for node1 in node0.children:
              doSomething(node1)
      
      T 1 Reply Last reply 13 Mar 2021, 06:48
      0
      • J JonB
        12 Mar 2021, 22:15

        @Taytoo

        for node0 in topLevelNodes:
            doSomething(node0)
            for node1 in node0.children:
                doSomething(node1)
        
        T Offline
        T Offline
        Taytoo
        wrote on 13 Mar 2021, 06:48 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.

        K J 2 Replies Last reply 13 Mar 2021, 07:03
        0
        • T Taytoo
          13 Mar 2021, 06:48

          @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.

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 13 Mar 2021, 07:03 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
            13 Mar 2021, 06:48

            @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.

            J Offline
            J Offline
            JonB
            wrote on 13 Mar 2021, 08:03 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

            1/5

            12 Mar 2021, 21:03

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved