QString search_text = ui->lineEdit->text();
if (search_text.trimmed().isEmpty())
{
return;
}
else
{
QTextDocument *document = ui->textEdit->document();
bool found = false;
QTextCursor highlight_cursor(document);
QTextCursor cursor(document);
//开始
cursor.beginEditBlock();
QTextCharFormat color_format(highlight_cursor.charFormat());
color_format.setForeground(Qt::red); //字体颜色
color_format.setBackground(Qt::blue); //背景颜色
while (!highlight_cursor.isNull() && !highlight_cursor.atEnd())
{
//查找指定的文本,匹配整个单词
highlight_cursor = document->find(search_text, highlight_cursor, QTextDocument::FindWholeWords);
if (!highlight_cursor.isNull())
{
if (!found)
found = true;
highlight_cursor.mergeCharFormat(color_format);
}
}
cursor.endEditBlock();
}
QTextEdit 匹配文本 进行高亮显示
猜你喜欢
转载自blog.csdn.net/hss2799/article/details/106123488
今日推荐
周排行