1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| void TextFinder::on_findButton_clicked()
{
initText();
QString searchString = ui->lineEdit->text();
QTextDocument *document = ui->textEdit->document();
QTextCursor highlightCursor(document);
QTextCursor cursor(document);
cursor.beginEditBlock();
QTextCharFormat plainFormat(highlightCursor.charFormat());
QTextCharFormat colorFormat = plainFormat;
colorFormat.setBackground(Qt::yellow);
QTextCharFormat colorFormat2 = plainFormat;
colorFormat2.setBackground(Qt::white);
while(!highlightCursor.isNull() && !highlightCursor.atEnd()){
highlightCursor = document->find(searchString, highlightCursor, QTextDocument::FindWholeWords);
if(!highlightCursor.isNull()){
highlightCursor.movePosition(QTextCursor::EndOfWord,QTextCursor::KeepAnchor);
highlightCursor.mergeCharFormat(colorFormat);
}
}
cursor.endEditBlock();
} |
Partager