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. Search in a subtree of QTreeWidget
Forum Updated to NodeBB v4.3 + New Features

Search in a subtree of QTreeWidget

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

    In QTreeWidget there is a function called findItems to find items in the tree with a specific value. However, I want to search in a subtree of the QTreeWidget. I have a pointer to the topnode of the subtree (a QTreeWidgetItem). To search in the tree I have so far thought about making a new tree which only consists of the subtree and use the findItems function, but I don't know how to this. addTopLevelItem doesn't seem to work for this. Is there an easy way to do this? Is there a better way to search in a subtree?

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      You could just write your own search function that recurses down into only the provided sub-tree.

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Aroch
        wrote on last edited by
        #3

        @
        QTreeWidgetItem * cur_item = ui->treeWidget->currentItem();
        QTreeWidgetItem * base_item = cur_item;
        while(base_item->parent() != 0)
        base_item = base_item->parent();

        int id_of_cur_top_level = ui->treeWidget->indexOfTopLevelItem(base_item);

        QList<QTreeWidgetItem*> list = ui->treeWidget->findItems("My item", Qt::MatchExactly | Qt::MatchRecursive, 0);

        QTreeWidgetItem* searched_item = 0;

        for(QList<QTreeWidgetItem*>::iterator it = list.begin();it != list.end(); ++it){
        base_item = *it;

        while(base_item->parent() != 0)
        base_item = base_item->parent();

        int i = ui->treeWidget->indexOfTopLevelItem(base_item);

        if(i == id_of_cur_top_level){
        searched_item = *it;
        break;
        }
        }

        if(searched_item != 0){
        // todo
        }
        @

        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