Random78952
Random78952

Reputation: 1640

How to know if a string exist in a item based ListView Qt C++

I want to know if a string exists in any item of a list view in C++ with Qt, how to do that ?

Upvotes: 0

Views: 745

Answers (1)

hauron
hauron

Reputation: 4668

Perhaps this little iterating function does what you're looking for:

bool doesContain(QListView *listView, QString expression)
{
    QAbstractItemModel* model = listView->model() ;
    int rowCount = model->rowCount();
    int columnCount = model->columnCount();

    for(int i = 0; i < rowCount; i++)
        for(int j = 0; j < columnCount; j++)
            if(model->index(i, j).data(Qt::DisplayRole).toString().contains(expression))
                return true;

    return false;
}

Upvotes: 3

Related Questions