<?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 (re-)select previous row in a QTreeView?]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">I have a QTreeView with single column and multiple row (lets say 5 rows). What I want to achieve is that if I select a row, I want to re-select previous row on some condition.</p>
<p dir="auto">Here is the code I have:</p>
<p dir="auto"><strong>MyWidget.h</strong></p>
<pre><code>#ifndef MYWIDGET_H
#define MYWIDGET_H

#include &lt;QWidget&gt;
#include &lt;QTreeView&gt;
#include &lt;QStandardItemModel&gt;
#include &lt;QStandardItem&gt;
#include &lt;QModelIndex&gt;
#include &lt;QHBoxLayout&gt;

class MyWidget : public QWidget
{
	Q_OBJECT

public:
	MyWidget(QWidget *parent = 0);
	~MyWidget();

private slots:
	void _OnTreeViewCurrentRowChanged(const QModelIndex &amp;rcqmiCurrIndex,
		const QModelIndex &amp;rcqmiPrevIndex);

private:
	QHBoxLayout *_pLayout;

	QTreeView *_pTreeView;
	QStandardItemModel *_pStandardItemModel;
	QStandardItem *_pStandardItem;
};

#endif
</code></pre>
<p dir="auto"><strong>MyWidget.cpp</strong></p>
<pre><code>#include "MyWidget.h"

static bool bCondition = false;

MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
	_pLayout = new QHBoxLayout(this);

	_pTreeView = new QTreeView(this);
	_pStandardItemModel = new QStandardItemModel(_pTreeView);
	_pStandardItem = new QStandardItem("Column A");

	_pLayout-&gt;addWidget(_pTreeView);

	_pStandardItemModel-&gt;setColumnCount(1);
	_pStandardItemModel-&gt;setHorizontalHeaderItem(0, _pStandardItem);

	_pTreeView-&gt;setModel(_pStandardItemModel);
	_pTreeView-&gt;setSelectionBehavior(QAbstractItemView::SelectRows);
	_pTreeView-&gt;setSelectionMode(QAbstractItemView::SingleSelection);
	_pTreeView-&gt;setEditTriggers(QAbstractItemView::NoEditTriggers);

	// add rows
	_pTreeView-&gt;selectionModel()-&gt;blockSignals(true);

	for (int i = 0; i &lt; 5; ++i)
	{
		QStandardItem *pStandardItem = new QStandardItem(
			QString("Row: %1").arg(i + 1));

		_pStandardItemModel-&gt;appendRow(pStandardItem);
	}

	_pTreeView-&gt;selectionModel()-&gt;blockSignals(false);

	// update view
	_pTreeView-&gt;viewport()-&gt;update();

	connect(_pTreeView-&gt;selectionModel(),
		SIGNAL(currentRowChanged(const QModelIndex &amp;, const QModelIndex &amp;)),
		this, SLOT(_OnTreeViewCurrentRowChanged(const QModelIndex &amp;,
		const QModelIndex &amp;)));
}

MyWidget::~MyWidget()
{

}

void MyWidget::_OnTreeViewCurrentRowChanged(
	const QModelIndex &amp;rcqmiCurrIndex, const QModelIndex &amp;rcqmiPrevIndex)
{
	if (bCondition) // some condition
	{
		_pTreeView-&gt;selectionModel()-&gt;blockSignals(true);

		// select previous index
		_pTreeView-&gt;selectionModel()-&gt;setCurrentIndex(
			rcqmiPrevIndex, QItemSelectionModel::SelectCurrent);

		_pTreeView-&gt;selectionModel()-&gt;blockSignals(false);

		// update view
		_pTreeView-&gt;viewport()-&gt;update();
	}

	bCondition = !bCondition;
}
</code></pre>
<p dir="auto">In <em>_OnTreeViewCurrentRowChanged(...)</em>, I can see that "selection" of tree view's selection model is updated with <code>QItemSelectionModel::setCurrentIndex(rcqmiPrevIndex, QItemSelectionModel::SelectCurrent);</code> but the tree view's row selection is NOT updated, still the <code>rcqmiCurrIndex</code> is selected.</p>
<p dir="auto">What am I missing here?</p>
<p dir="auto">Any help is much appreciated. Thanks in advance.</p>
]]></description><link>https://forum.qt.io/topic/70543/how-to-re-select-previous-row-in-a-qtreeview</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 14:19:59 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/70543.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 24 Aug 2016 07:55:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to (re-)select previous row in a QTreeView? on Wed, 24 Aug 2016 08:32:39 GMT]]></title><description><![CDATA[<p dir="auto">And perhaps the protected function of QTreeView might also be of use.<br />
You will have to subclass it though.</p>
<pre><code>virtual void  currentChanged(const QModelIndex &amp;current, const QModelIndex &amp;previous) 
</code></pre>
]]></description><link>https://forum.qt.io/post/343873</link><guid isPermaLink="true">https://forum.qt.io/post/343873</guid><dc:creator><![CDATA[Jan-Willem]]></dc:creator><pubDate>Wed, 24 Aug 2016 08:32:39 GMT</pubDate></item><item><title><![CDATA[Reply to How to (re-)select previous row in a QTreeView? on Wed, 24 Aug 2016 08:16:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/soma_yarram">@<bdi>soma_yarram</bdi></a> said:</p>
<blockquote>
<p dir="auto">_pTreeView-&gt;selectionModel()-&gt;blockSignals(true);</p>
</blockquote>
<p dir="auto">Hi,<br />
maybe the row selection depends on a signal and u block it:) ?</p>
]]></description><link>https://forum.qt.io/post/343870</link><guid isPermaLink="true">https://forum.qt.io/post/343870</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Wed, 24 Aug 2016 08:16:59 GMT</pubDate></item></channel></rss>