标题 QT中在文本编辑框中找到查找特定字符串,然后将字符串所在整行字符串添加颜色

本文介绍了如何在QT应用程序中,使用C++代码实现在文本编辑框内查找特定字符串,并将含有该字符串的整行设置为特定颜色,以达到高亮显示的效果。
摘要由CSDN通过智能技术生成
    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())
            {
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值