<?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 revert to previous selection when an undo operation happens in QTreeView]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I'm quite new to Qt. So in my current GUI, undoing an operation doesn't return selection back to the previously selected one, which means the previous one doesn't get highlighted.</p>
<p dir="auto">For example, if I select one part of the model, do some operations, then select another part and undo it, the selection doesn't go back to the previous one, so it must be selected again before I can do other stuff.</p>
]]></description><link>https://forum.qt.io/topic/137284/how-to-revert-to-previous-selection-when-an-undo-operation-happens-in-qtreeview</link><generator>RSS for Node</generator><lastBuildDate>Mon, 08 Jun 2026 17:46:37 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/137284.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 17 Jun 2022 06:40:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to revert to previous selection when an undo operation happens in QTreeView on Wed, 22 Jun 2022 07:42:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/garcia_g">@<bdi>garcia_g</bdi></a><br />
<code>QTreeView</code> has <a href="https://doc-snapshots.qt.io/qt5-5.15/qabstractitemview.html#selectionModel" target="_blank" rel="noopener noreferrer nofollow ugc">QItemSelectionModel *QAbstractItemView::selectionModel() const</a>.  And that has e.g. <a href="https://doc-snapshots.qt.io/qt5-5.15/qitemselectionmodel.html#select" target="_blank" rel="noopener noreferrer nofollow ugc">void QItemSelectionModel::select(const QModelIndex &amp;index, QItemSelectionModel::SelectionFlags command)</a>.  So if you remember <a href="https://doc-snapshots.qt.io/qt5-5.15/qtreeview.html#selectedIndexes" target="_blank" rel="noopener noreferrer nofollow ugc">QModelIndexList QTreeView::selectedIndexes() const</a> you can use that to re-select.</p>
<p dir="auto">See also e.g. <a href="https://stackoverflow.com/questions/4967660/selecting-a-row-in-qtreeview-programmatically" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/4967660/selecting-a-row-in-qtreeview-programmatically</a> or <a href="https://forum.qt.io/topic/11170/solved-how-to-set-an-item-in-a-qtreeview-as-selected">https://forum.qt.io/topic/11170/solved-how-to-set-an-item-in-a-qtreeview-as-selected</a>.</p>
<p dir="auto">While you are implementing this.  Are you aware that Qt has a <a href="https://doc-snapshots.qt.io/qt5-5.15/qundostack.html" target="_blank" rel="noopener noreferrer nofollow ugc">QUndoStack Class</a>?  You might want to use this.  It does <em>not</em> provide the undo "operations" --- e.g. adding/deleting/selecting your elements --- you still have to write the code for that, just as now.  But it gives you the <em>infrastructure</em> for <em>managing</em> the undo/redo operations, especially if you will allow multi-action undo/redo, which otherwise you will have to write yourself.</p>
]]></description><link>https://forum.qt.io/post/718545</link><guid isPermaLink="true">https://forum.qt.io/post/718545</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 22 Jun 2022 07:42:12 GMT</pubDate></item><item><title><![CDATA[Reply to How to revert to previous selection when an undo operation happens in QTreeView on Wed, 22 Jun 2022 07:06:03 GMT]]></title><description><![CDATA[<p dir="auto">Let's say I rotated the selected item and now I want to undo this rotation. If I press "ctrl+z" to undo, then the item is deselected, i.e. "un-highlighted", and my selected item is null in this case. So how can I remain the item selected even when the "undo" operation happens such that it is the same as if I had clicked on it?</p>
]]></description><link>https://forum.qt.io/post/718540</link><guid isPermaLink="true">https://forum.qt.io/post/718540</guid><dc:creator><![CDATA[garcia_g]]></dc:creator><pubDate>Wed, 22 Jun 2022 07:06:03 GMT</pubDate></item><item><title><![CDATA[Reply to How to revert to previous selection when an undo operation happens in QTreeView on Fri, 17 Jun 2022 07:45:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/garcia_g">@<bdi>garcia_g</bdi></a><br />
Hello and welcome.</p>
<p dir="auto">So how do you do your "undo" operation?  If you want, say, selection to revert to what it was before your "operations" then you must remember what was selected and reselect that as part of the "undo".</p>
]]></description><link>https://forum.qt.io/post/717983</link><guid isPermaLink="true">https://forum.qt.io/post/717983</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 17 Jun 2022 07:45:12 GMT</pubDate></item></channel></rss>