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?

0

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.