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 (re-)select previous row in a QTreeView?
Forum Updated to NodeBB v4.3 + New Features

How to (re-)select previous row in a QTreeView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.1k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    soma_yarram
    wrote on last edited by
    #1

    Hello,

    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.

    Here is the code I have:

    MyWidget.h

    #ifndef MYWIDGET_H
    #define MYWIDGET_H
    
    #include <QWidget>
    #include <QTreeView>
    #include <QStandardItemModel>
    #include <QStandardItem>
    #include <QModelIndex>
    #include <QHBoxLayout>
    
    class MyWidget : public QWidget
    {
    	Q_OBJECT
    
    public:
    	MyWidget(QWidget *parent = 0);
    	~MyWidget();
    
    private slots:
    	void _OnTreeViewCurrentRowChanged(const QModelIndex &rcqmiCurrIndex,
    		const QModelIndex &rcqmiPrevIndex);
    
    private:
    	QHBoxLayout *_pLayout;
    
    	QTreeView *_pTreeView;
    	QStandardItemModel *_pStandardItemModel;
    	QStandardItem *_pStandardItem;
    };
    
    #endif
    

    MyWidget.cpp

    #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->addWidget(_pTreeView);
    
    	_pStandardItemModel->setColumnCount(1);
    	_pStandardItemModel->setHorizontalHeaderItem(0, _pStandardItem);
    
    	_pTreeView->setModel(_pStandardItemModel);
    	_pTreeView->setSelectionBehavior(QAbstractItemView::SelectRows);
    	_pTreeView->setSelectionMode(QAbstractItemView::SingleSelection);
    	_pTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    
    	// add rows
    	_pTreeView->selectionModel()->blockSignals(true);
    
    	for (int i = 0; i < 5; ++i)
    	{
    		QStandardItem *pStandardItem = new QStandardItem(
    			QString("Row: %1").arg(i + 1));
    
    		_pStandardItemModel->appendRow(pStandardItem);
    	}
    
    	_pTreeView->selectionModel()->blockSignals(false);
    
    	// update view
    	_pTreeView->viewport()->update();
    
    	connect(_pTreeView->selectionModel(),
    		SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)),
    		this, SLOT(_OnTreeViewCurrentRowChanged(const QModelIndex &,
    		const QModelIndex &)));
    }
    
    MyWidget::~MyWidget()
    {
    
    }
    
    void MyWidget::_OnTreeViewCurrentRowChanged(
    	const QModelIndex &rcqmiCurrIndex, const QModelIndex &rcqmiPrevIndex)
    {
    	if (bCondition) // some condition
    	{
    		_pTreeView->selectionModel()->blockSignals(true);
    
    		// select previous index
    		_pTreeView->selectionModel()->setCurrentIndex(
    			rcqmiPrevIndex, QItemSelectionModel::SelectCurrent);
    
    		_pTreeView->selectionModel()->blockSignals(false);
    
    		// update view
    		_pTreeView->viewport()->update();
    	}
    
    	bCondition = !bCondition;
    }
    

    In _OnTreeViewCurrentRowChanged(...), I can see that "selection" of tree view's selection model is updated with QItemSelectionModel::setCurrentIndex(rcqmiPrevIndex, QItemSelectionModel::SelectCurrent); but the tree view's row selection is NOT updated, still the rcqmiCurrIndex is selected.

    What am I missing here?

    Any help is much appreciated. Thanks in advance.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @soma_yarram said:

      _pTreeView->selectionModel()->blockSignals(true);

      Hi,
      maybe the row selection depends on a signal and u block it:) ?

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jan-Willem
        wrote on last edited by
        #3

        And perhaps the protected function of QTreeView might also be of use.
        You will have to subclass it though.

        virtual void  currentChanged(const QModelIndex &current, const QModelIndex &previous) 
        
        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved