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 strip unselected items from a QTreeWidgetItem branch
Qt 6.11 is out! See what's new in the release blog

how to strip unselected items from a QTreeWidgetItem branch

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 486 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.
  • C Offline
    C Offline
    codeZombie
    wrote on last edited by codeZombie
    #1

    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?

    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