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. QTreeWidgetItems search issue
Qt 6.11 is out! See what's new in the release blog

QTreeWidgetItems search issue

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.0k 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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #1

    Hi! I want to search items in QTreeWidget. The problem is, it doesn't hide the items that don't match the string.

    QString data = "Some text";
    QList<QTreeWidgetItem*> itemsList = treeWidget->findItems(data, Qt::MatchContains | Qt::MatchRecursive, 0);
        for (QTreeWidgetItem *item : itemsList)
        {
            if (!data.isEmpty()) {
                if (item->text(0) == data) {
                    item->setHidden(false);
                } else {
                    item->setHidden(true);
                }
            } else {
                item->setHidden(false);
            }
        }
    

    I want to hide items when they are not matching, and display the matching one. Thanks.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi,

      You are only looping on items that do match your search so the others will stay untouched.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Cobra91151C 2 Replies Last reply
      2
      • SGaistS SGaist

        Hi,

        You are only looping on items that do match your search so the others will stay untouched.

        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by
        #3

        @SGaist

        Ok. Thanks. I will fix it later and reply.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          You are only looping on items that do match your search so the others will stay untouched.

          Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by
          #4

          @SGaist

          Thank you. I have fixed the issue. I post code here, so people can find a solution.

          Code:

          for (int i = 0; i < treeWidget->topLevelItemCount(); i++) {
                  QTreeWidgetItem *item = treeWidget->topLevelItem(i);
                  QList<QTreeWidgetItem*> itemsList = treeWidget->findItems(data, Qt::MatchContains | Qt::MatchRecursive, 0);
                  for (QTreeWidgetItem *searchedItem : itemsList)
                  {
                      if (!data.isEmpty()) {
                          searchedItem->setHidden(false);
                          item->setHidden(true);
                      } else {
                          item->setHidden(false);
                      }
                  }
              }
          
          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