how to strip unselected items from a QTreeWidgetItem branch



  • I need to clone a branch in a QTreeWidget where only selected items are returned. For example...

    menu 1
     |-not a menu 1
     |-not a menu 2 (*)
     |-menu 2 (*)
         |-not a menu 3
         |-menu 3
             |-not a menu 4 (*)
    

    If I call tree->selectedItems() I get [not a menu 2, menu 2, not a menu 4]. I then clone the highest level items. Code abbreviated...

    bool highestLevel(QTreeWidgetItem *item);
    QTreeWidgetItem* stripUnselected(QTreeWidgetItem *item);
    
    for (i < selectedTotal) {
      // only want highest level items
      if (!highestLevel(items[i])) continue;
      clone = items[i]->clone();
      onlySelected = stripUnselected(clone);
    }
    

    The highestLevel function tests whether the item has a parent selected. If there's a selected parent it skips ahead to the next iteration. The stipUnselected function is where I'm having trouble. How can I loop through the entire cloned branch, detect unselected items, remove them, but also retain their children?

    In the example above the onlySelected item would contain...

    // first iteration
    onlySelected = not a menu 2
    // second iteration
    onlySelected = menu 2 > not a menu 4

    How can I code the stripUnselected function?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.