如何使用QT C++在QTableView中找出包含QString的单元格的索引(即行号和列号)?查找包含值的单元格的索引并突出显示QTableView中的所有单元格
(在QTableView中的细胞P.S.:Without点击)
如何使用QT C++在QTableView中找出包含QString的单元格的索引(即行号和列号)?查找包含值的单元格的索引并突出显示QTableView中的所有单元格
(在QTableView中的细胞P.S.:Without点击)
可以使用findItems()
功能找到你的。
findItems()
函数返回给定列中使用给定标志匹配给定文本的项目列表。
for (int index = 0; index < model->columnCount(); index++)
{
QList<QStandardItem*> foundLst = model->findItems("YourText", Qt::MatchExactly, index);
}
如果你想找到的项目指标,并强调它使用此代码:
for (int index = 0; index < model->columnCount(); index++)
{
QList<QStandardItem*> foundLst = model->findItems("YourText", Qt::MatchExactly, index);
int count = foundLst.count();
if(count>0)
{
for(int k=0; k<count; k++)
{
QModelIndex modelIndex = model->indexFromItem(foundLst[k]);
qDebug()<< "column= " << index << "row=" << modelIndex.row();
((QStandardItemModel*)modelIndex.model())->item(modelIndex.row(),index)->setData(QBrush(Qt::green),Qt::BackgroundRole);
}
}
}
更多信息:
QTableView:本QTableView
类提供的默认模型/视图实现一张桌子视图。
QStandardItemModel:QStandardItemModel
类提供了一种用于存储自定义数据的通用模型。
您的意思是搜索和查找表中的值? – aghilpro
搜索后我想要表 – annie
中的值的位置我更新我的答案。这行添加:'((QStandardItemModel *)modelIndex.model()) - > item(modelIndex.row(),index) - > setData(QBrush(Qt :: green),Qt :: BackgroundRole);' – aghilpro