QTableWidget find a row through userdata

牧云@^-^@ 提交于 2019-12-13 01:29:53

问题


QTableWidget have a method to search for a row with user data?

Something like:

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

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

回答1:


You can use 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 ...
}


来源:https://stackoverflow.com/questions/11851854/qtablewidget-find-a-row-through-userdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!