开发者

how to get selected rows in QTableView

开发者 https://www.devze.com 2023-03-03 13:43 出处:网络
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

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);
开发者_JAVA百科

and if you have your own way of selecting can you explain how it works. Thanks for the help!


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


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


try:

QModelIndexList indexList = yourTableView->selectionModel()->selectedIndexes();
int row;
foreach (QModelIndex index, indexList) {
    row = index.row();
    ....
}


Since you use

setSelectionBehavior(QAbstractItemView::SelectRows);
setSelectionMode(QAbstractItemView::SingleSelection);

so only one row can be selected each time, then you can try this:

auto rowList = yourTableView->selectionModel()->selectedRows();
if(rowList.count() > 0)
    int rowNumber = rowList.constFirst().row();
else
    // no row is selected
0

精彩评论

暂无评论...
验证码 换一张
取 消