Vadim Kiselev
Vadim Kiselev

Reputation: 1050

How to move programmatically to selected row in Qt?

I use QTreeView and QSortFilterProxyModel

// Here I determine the index, that was saved before (_lastAddObjectIndex - QModelIndex)
QModelIndex next_index = _proxyModel->index(_lastAddObjectIndex.row(), 0);
    
// Here I select the row programmatically, and after that I'd like to move to that row (because table might have many rows)
view->selectionModel()->select(next_index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows | QItemSelectionModel::SelectCurrent);

Upvotes: 1

Views: 214

Answers (1)

László Papp
László Papp

Reputation: 53155

I assume by moving, you mean scrolling. If so, you can achieve this by using this API:

view->scrollTo(next_index);

You can even change the scroll hint if you pass a second parameter to the method. This depends on whether you are happy with the default value, which just makes sure that the item is visible.

You can refer to the documentation for fine-tuning this behaviour further in case you need to.

Upvotes: 1

Related Questions