2014-01-06 36 views
0

我想通過鍵入一個字符串來過濾所有數據,聽起來很簡單。 這是我走到這一步:Qt - 拆分並使用lineEdit中的字符串作爲regExp

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)))); 
} 

這工作得很好,如果我只是尋找一個字或如果我搜索「*」字之間,如果他們在正確的順序是。 但我怎樣才能搜索多個單詞和單詞的順序應該不重要?

回答

3

您需要使用Positive Lookahead功能,並使用輸入的所有單詞構建正則表達式字符串。下面是一個簡單的例子(假設輸入one two three):

QRegExp re("^(?=.*one)(?=.*two)(?=.*three).*$"); 
qDebug() << re.exactMatch("two three one four"); // returns true 
相關問題