문제

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?

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top