<?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[Track selection in QListWidget after undo&#x2F;redo operations]]></title><description><![CDATA[<p dir="auto">I have a QListWidget containing anywhere from 1 to 511 QListWidgetItems. I can add a new item and perform an undo and the new item is removed and a redo to add it back.  This is done by saving the state of the vector containing all the QListWidgetItems.  I perform the undo/redo by the following:</p>
<pre><code>QlistWidget-&gt;clear();
</code></pre>
<p dir="auto">and then reloading the data from the vector.</p>
<p dir="auto">The problem is if there are selected items in the QListWidget.  The selected items must be tracked in the undo/redo operations. I do save the selected items into a QList before clearing the QlistWidget. The problem is re-selecting the item after a redo. Since I clear the list and reload it the QListWidgetItem saved doesn't match the new list so it selects the wrong item in the QListWidget.</p>
<p dir="auto">I save the selected items by doing the following:</p>
<pre><code>QList&lt;QListWidgetItems*&gt; selectedIndices = QListWidget-&gt;selectedItems();
</code></pre>
<p dir="auto">then clear the QListWidget:</p>
<pre><code>QListWidget-&gt;clear();
</code></pre>
<p dir="auto">reload the QListWidget and then set the selected items:</p>
<pre><code>for (QListWidgetItems *items : selectedIndices )
{
    QListWidget-&gt;setCurrentItem(items);
}

</code></pre>
]]></description><link>https://forum.qt.io/topic/163663/track-selection-in-qlistwidget-after-undo-redo-operations</link><generator>RSS for Node</generator><lastBuildDate>Fri, 10 Apr 2026 05:37:38 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/163663.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 06 Nov 2025 15:12:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Track selection in QListWidget after undo&#x2F;redo operations on Thu, 06 Nov 2025 18:13:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/gabello306">@<bdi>gabello306</bdi></a><br />
But your <code>selectedIndices</code>  does not hold indices!  Don't save pointers to anything, and not the whole <code>QListWidgetItem</code>.  Save indexes, or "unique key" if it's appropriate and unique.  I don't know whether with <a href="https://doc.qt.io/qt-6/qlistview.html#selectedIndexes" target="_blank" rel="noopener noreferrer nofollow ugc">QModelIndexList QListView::selectedIndexes()</a> you can save and reuse a <code>QModelIndex</code>, quite possibly not, but since you have a simple list you can just save an <code>int</code> of its index.  Of course you also need to restore sort order if that is dynamic....</p>
]]></description><link>https://forum.qt.io/post/833386</link><guid isPermaLink="true">https://forum.qt.io/post/833386</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 06 Nov 2025 18:13:06 GMT</pubDate></item></channel></rss>