2017-08-07 135 views
2

如何使用QT C++在QTableView中找出包含QString的单元格的索引(即行号和列号)?查找包含值的单元格的索引并突出显示QTableView中的所有单元格

(在QTableView中的细胞P.S.:Without点击)

+0

您的意思是搜索和查找表中的值? – aghilpro

+0

搜索后我想要表 – annie

+0

中的值的位置我更新我的答案。这行添加:'((QStandardItemModel *)modelIndex.model()) - > item(modelIndex.row(),index) - > setData(QBrush(Qt :: green),Qt :: BackgroundRole);' – aghilpro

回答

1

可以使用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类提供的默认模型/视图实现一张桌子视图。

QStandardItemModelQStandardItemModel类提供了一种用于存储自定义数据的通用模型。

+0

谢谢@aghilpro,你的解决方案解决了我的问题。 – annie

+0

@annie欢迎我的朋友。 – aghilpro

+0

您的解决方案为相同字符串的多次出现返回相同的索引。例如,如果表中第2行和第4行中包含字符串“apple”,则程序将两个事件的索引返回为2,但我希望打印两个事件的索引,即必须打印2和4 。你能让我知道我该怎么做吗? – annie

相关问题