how to get selected rows in QTableView

2019-01-31 07:35发布

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!

3条回答
\"骚年 ilove
2楼-- · 2019-01-31 07:43

try:

QModelIndexList indexList = yourTableView->selectionModel()->selectedIndexes();
int row;
foreach (QModelIndex index, indexList) {
    row = index.row();
    ....
}
查看更多
爷的心禁止访问
3楼-- · 2019-01-31 07:49

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)
...
查看更多
一纸荒年 Trace。
4楼-- · 2019-01-31 08:01

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();
}
查看更多
登录 后发表回答