0

I have a QMainWindow containing a QTableView as its centralwidget.

I populate this QTableView by setting a model (which is derived from QAbstractTableModel).

The selection behavior for the QTableView is set to QAbstractItemView::SelectRows. This means that if I click in a cell, the entire row is selected (and is highlighted).

I would like to be able to focus/highlight a row in the QTableView programatically. In other words, I would like to focus/highlight a row without the user clicking on it. How can this be done, do I 'fake' a click in a cell?

Lieuwe
  • 1,734
  • 2
  • 27
  • 41

1 Answers1

2

You can achieve this by using QItemSelectionModel of your table view, which you can get by calling the QTableView::selectionModel() method. QItemSelectionModel has public slot QItemSelection::select(QModelIndex, QItemSelectionModel::SelectionFlags) which is changing current selection when called. So when you want to highlight particular row you can do this:

QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows;
QModelIndex index = m_tableView->model()->index(rowIndex, 0);
m_tableView->selectionModel()->select(index, flags);

You can find selection flags description here.

teapot_of_wine
  • 516
  • 3
  • 18