QItemSelectionModel* selectionModel = myTableView->selectionModel(); QModelIndexList selectedIndexes = selectionModel->selectedRows(); foreach (const QModelIndex& index, selectedIndexes) { qDebug() << "Selected row: " << index.row(); }
QItemSelectionModel* selectionModel = myTreeView->selectionModel(); QModelIndexList selectedIndexes = selectionModel->selectedRows(0, myTreeView->rootIndex()); foreach (const QModelIndex& index, selectedIndexes) { qDebug() << "Selected row: " << index.row(); }This example retrieves the selection model for a QTreeView and uses selectedRows() to get a list of all of the selected rows that are children of the root index. It then prints out each row that was selected. Both of these examples use the Qt library and require the inclusion of the appropriate header files.