how to get selected rows in QTableView
90,437
Solution 1
The method selectionModel()
return a QItemSelectionModel
.
You can use QItemSelectionModel
class to check/change/other selection(s)
Example:
QItemSelectionModel *select = table->selectionModel();
select->hasSelection() //check if has selection
select->selectedRows() // return selected row(s)
select->selectedColumns() // return selected column(s)
...
Solution 2
Check selectedRows
method of the QItemSelectionModel
Class .
QModelIndexList selection = yourTableView->selectionModel()->selectedRows();
// Multiple rows can be selected
for(int i=0; i< selection.count(); i++)
{
QModelIndex index = selection.at(i);
qDebug() << index.row();
}
Solution 3
try:
QModelIndexList indexList = yourTableView->selectionModel()->selectedIndexes();
int row;
foreach (QModelIndex index, indexList) {
row = index.row();
....
}
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on March 23, 2020Comments
-
Admin over 4 years
After watching many threads about getting selected rows numbers, I am really confused.
How do you get ROW numbers in
QTableView
usingQStandardItemModel
I used below selection model and behavior assetSelectionBehavior(QAbstractItemView::SelectRows); setSelectionMode(QAbstractItemView::SingleSelection);
and if you have your own way of selecting can you explain how it works. Thanks for the help!
-
Mikhail over 10 yearsI wonder if you parse a column, will you drop the same row twice (or more likely some other row).
-
Thomas Williams about 7 yearsIs it possible to do this with the model name instad of the table name?
-
user202729 almost 6 yearsFor reference: the method was inherited from
QAbstractItemView
.