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. how to traverse all the items of QListWidget/QTreeWidget?

how to traverse all the items of QListWidget/QTreeWidget?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 6.4k 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.
  • O Offline
    O Offline
    opengpu2
    wrote on last edited by
    #1

    how to traverse all the items of QListWidget/QTreeWidget?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by mcosta
      #2

      Hi,

      for a QListWidget you can do something like

      // myWidget is a QListWidget*
      for (int i = 0; myWidget->count(); ++i) {
          QListWidgetItem *item = myWidget->item(i);
          // Do something with item
      }
      

      for a QTreeWidget

      // myWidget is a QTreeWidgetItem*
      void traverseNode(QTreeWidgetItem *item) {
          // Do something with item
          for (int i = 0; i < item->childCount(); ++i)
              traverseNode(item->child(i));
      }
      
      for (int i = 0; i < myWidget->topLevelItemCount(); ++i) {
          QTreeWidgetItem *item = myWidget->topLevelItem(i);
          traverseNode(item);
      }
      

      For a tree usually you have to decide to process the item itself before or after its child.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      O 1 Reply Last reply
      0
      • M mcosta

        Hi,

        for a QListWidget you can do something like

        // myWidget is a QListWidget*
        for (int i = 0; myWidget->count(); ++i) {
            QListWidgetItem *item = myWidget->item(i);
            // Do something with item
        }
        

        for a QTreeWidget

        // myWidget is a QTreeWidgetItem*
        void traverseNode(QTreeWidgetItem *item) {
            // Do something with item
            for (int i = 0; i < item->childCount(); ++i)
                traverseNode(item->child(i));
        }
        
        for (int i = 0; i < myWidget->topLevelItemCount(); ++i) {
            QTreeWidgetItem *item = myWidget->topLevelItem(i);
            traverseNode(item);
        }
        

        For a tree usually you have to decide to process the item itself before or after its child.

        O Offline
        O Offline
        opengpu2
        wrote on last edited by
        #3

        @mcosta thank you
        does this->findItems() faster?

        K 1 Reply Last reply
        0
        • O opengpu2

          @mcosta thank you
          does this->findItems() faster?

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @opengpu2
          It depends what your intend is. mcosta answered your initial question.
          If you are searching something specific and you know the details anyhow findItems() is the better choices.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            As @koahnig said, findItems() find items by name.
            If you want to traverse the List/Tree you cannot use it.

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            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