查找包含值的单元格的索引并突出显示 QTableView 中的所有这些单元格
Finding index of a cell containing a value and highlighting all those cells in QTableView
我们如何使用 QT c++ 找出 QTableView 中包含 QString 的单元格的索引(即行号和列号)?
(P.S.:没有点击qtableview中的单元格)
您可以使用 findItems()
功能来查找您的手机。
findItems()
函数 returns 在给定列中使用给定标志匹配给定文本的项目列表。
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
class 提供了 table 视图的默认 model/view 实现。
QStandardItemModel: QStandardItemModel
class 提供了用于存储自定义数据的通用模型。
我们如何使用 QT c++ 找出 QTableView 中包含 QString 的单元格的索引(即行号和列号)?
(P.S.:没有点击qtableview中的单元格)
您可以使用 findItems()
功能来查找您的手机。
findItems()
函数 returns 在给定列中使用给定标志匹配给定文本的项目列表。
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
class 提供了 table 视图的默认 model/view 实现。
QStandardItemModel: QStandardItemModel
class 提供了用于存储自定义数据的通用模型。