Home > Back-end >  How to move programmatically to selected row in Qt?
How to move programmatically to selected row in Qt?

Time:07-25

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);

CodePudding user response:

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.

  • Related