QTableWidget通过userdata查找一行

时间:2012-08-07 18:24:38

标签: qt find row qtablewidget user-data

QTableWidget有一种方法来搜索包含用户数据的行吗?

类似的东西:

//set user data
row->setData(0, Qt::UserRole, "ID001");

//find row by user data
int rowIndex = table->findByData("ID001");

1 个答案:

答案 0 :(得分:3)

您可以使用QAbstractItemModel::match()

QAbstractItemModel *model = table->model();
QModelIndexList matches = model->match( model->index(0,0), Qt::UserRole, "ID001" )

foreach( const QModelIndex &index, matches )
{
    QTableWidgetItem *item = table->item( index.row(), index.column() )
    // Do something with your new-found item ...
}