如何在QTableView中获取选定的行

时间:2011-05-08 12:45:13

标签: qt row qtableview

看了很多关于获取选定行号的线程后,我真的很困惑。

如何使用QTableView QStandardItemModel获取行号,我使用下面的选择模型和行为

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

如果您有自己的选择方式,可以解释它的工作原理。 谢谢你的帮助!

3 个答案:

答案 0 :(得分:54)

方法selectionModel()返回QItemSelectionModel

您可以使用QItemSelectionModel课程来检查/更改/选择其他选择

示例:

QItemSelectionModel *select = table->selectionModel();

select->hasSelection() //check if has selection
select->selectedRows() // return selected row(s)
select->selectedColumns() // return selected column(s)
...

答案 1 :(得分:11)

检查selectedRows类的QItemSelectionModel方法。

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

答案 2 :(得分:5)

尝试:

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