查找包含值的单元格的索引并在 QTableView 中突出显示所有这些单元格 [英] Finding index of a cell containing a value and highlighting all those cells in QTableView

查看:107
本文介绍了查找包含值的单元格的索引并在 QTableView 中突出显示所有这些单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何使用 QT c++ 在 QTableView 中找出包含 QString 的单元格的索引(即行号和列号)?

How can we find out the index (i.e both row and column numbers) of a cell containing a QString in a QTableView using QT c++?

(P.S.:没有点击qtableview中的单元格)

(P.S.:Without clicking on the cell in qtableview)

推荐答案

您可以使用 findItems() 函数来查找您的单元格.

You can use findItems() function to find your cell.

findItems() 函数返回在给定列中使用给定标志与给定文本匹配的项目列表.

findItems() function returns a list of items that match the given text, using the given flags, in the given column.

for (int index = 0; index < model->columnCount(); index++)
{
    QList<QStandardItem*> foundLst = model->findItems("YourText", Qt::MatchExactly, index);
}

如果您想获取已找到项目的索引并突出显示它,请使用以下代码:

If you want to get index of found item and highlight it use this code:

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 类提供表视图的默认模型/视图实现.

QTableView: The QTableView class provides a default model/view implementation of a table view.

QStandardItemModel:QStandardItemModel 类提供用于存储自定义数据的通用模型.

QStandardItemModel: The QStandardItemModel class provides a generic model for storing custom data.

这篇关于查找包含值的单元格的索引并在 QTableView 中突出显示所有这些单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆