QString idString = idTextEdit->textCursor().selectedText().trimmed();//idString为文本编辑区中光标选中高亮内容
if (idString.isEmpty()) //判断是否为空
{
QMessageBox::information(this, "提示", "无法找到");
}
else
{
QTextDocument *document = currentLogEdit->document(); //将文本编辑区转为QTextDocument对象
QTextCursor highLightCursor(document); //高亮光标
QTextCursor cursor(document); //搜索光标
QTextCursor textBlockCursor(document); //整行文本光标
QColor colorText = QColorDialog::getColor( Qt::red, this, "颜色对话框" ); //跳出颜色对话框,以便颜色选择
//开始光标搜索
cursor.beginEditBlock();
while (!highLightCursor.isNull() && !highLightCursor.atEnd())
{
highLightCursor = document->find(idString, highLightCursor, QTextDocument::FindWholeWords);
if (!highLightCursor.isNull())
{
标题 QT中在文本编辑框中找到查找特定字符串,然后将字符串所在整行字符串添加颜色
最新推荐文章于 2024-07-17 14:12:18 发布
本文介绍了如何在QT应用程序中,使用C++代码实现在文本编辑框内查找特定字符串,并将含有该字符串的整行设置为特定颜色,以达到高亮显示的效果。
摘要由CSDN通过智能技术生成