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();
    ....
}
Share:
90,437
Admin
Author by

Admin

Updated on March 23, 2020

Comments

  • Admin
    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 using QStandardItemModel I used below selection model and behavior as

    setSelectionBehavior(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
    Mikhail over 10 years
    I wonder if you parse a column, will you drop the same row twice (or more likely some other row).
  • Thomas Williams
    Thomas Williams about 7 years
    Is it possible to do this with the model name instad of the table name?
  • user202729
    user202729 almost 6 years
    For reference: the method was inherited from QAbstractItemView.