<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[how to strip unselected items from a QTreeWidgetItem branch]]></title><description><![CDATA[<p dir="auto">I need to clone a branch in a QTreeWidget where only selected items are returned.  For example...</p>
<pre><code>menu 1
 |-not a menu 1
 |-not a menu 2 (*)
 |-menu 2 (*)
     |-not a menu 3
     |-menu 3
         |-not a menu 4 (*)
</code></pre>
<p dir="auto">If I call tree-&gt;selectedItems() I get [not a menu 2, menu 2, not a menu 4].  I then clone the highest level items.  Code abbreviated...</p>
<pre><code>bool highestLevel(QTreeWidgetItem *item);
QTreeWidgetItem* stripUnselected(QTreeWidgetItem *item);

for (i &lt; selectedTotal) {
  // only want highest level items
  if (!highestLevel(items[i])) continue;
  clone = items[i]-&gt;clone();
  onlySelected = stripUnselected(clone);
}
</code></pre>
<p dir="auto">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?</p>
<p dir="auto">In the example above the onlySelected item would contain...</p>
<p dir="auto">// first iteration<br />
onlySelected = not a menu 2<br />
// second iteration<br />
onlySelected = menu 2 &gt; not a menu 4</p>
<p dir="auto">How can I code the stripUnselected function?</p>
]]></description><link>https://forum.qt.io/topic/79861/how-to-strip-unselected-items-from-a-qtreewidgetitem-branch</link><generator>RSS for Node</generator><lastBuildDate>Tue, 01 Sep 2026 22:20:26 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/79861.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 01 Jun 2017 22:02:24 GMT</pubDate><ttl>60</ttl></channel></rss>