Question

I want to filter all my data by typing a string, sounds simple. This is what I got so far:

stringToSearch.replace( QRegExp(" "), "|" );

QRegExp regExp(stringToSearch,Qt::CaseInsensitive, QRegExp::Wildcard); 

model->removeRows(0,model->rowCount());
for(int row = 0; row < stringsInTable.filter(regExp).count(); row++)
{
    model->appendRow(new QStandardItem(QString(stringsInTable.filter(regExp).at(row))));
}

This works fine if I just search for one word or if I search with '*' between the words if they come in correct order that is. But how can I search for multiple words and the order of the words should not matter?

Était-ce utile?

La solution

You need to use Positive Lookahead feature and build your regexp string using all words entered. Here's a quick example (let's suppose input one two three):

QRegExp re("^(?=.*one)(?=.*two)(?=.*three).*$");
qDebug() << re.exactMatch("two three one four"); // returns true
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top