QTextCursor::setPosition: Position ‘ ‘ out of range

本文介绍了一种解决在使用Qt编写程序时遇到的QTextCursor::setPosition超出范围错误的方法,通过将setPosition替换为movePosition,确保了消息输出框中文字颜色的正确设置和定位。

使用QT编写一个个性化消息输出框是发生:

QTextCursor::setPosition: Position ‘40’ out of range
QTextCursor::setPosition: Position ‘119’ out of range
QTextCursor::setPosition: Position ‘171’ out of range
QTextCursor::setPosition: Position ‘162’ out of range
QTextCursor::setPosition: Position ‘175’ out of range
QTextCursor::setPosition: Position ‘216’ out of range

原始代码:

void OutPutMsgToConsle(ConSleLevel level,const QString &strText)
{
   
   
//日志等级大于配置文件允许的最高等级,就返回
    //可以用于消息弹出时中止此函数
//    if (level > LocalConfig::GetInstance().GetCfgConsleLevel())
//    {
   
   
//        return;
//    }

    exportMessage->append(strText);

    //选中需要显示颜色的文字
    QTextCursor cursor = exportMessage->textCursor();
    cursor.setPosition(cursor.position(),QTextCursor::MoveAnchor);//待删除
    cursor.setPosition(cursor.position(</
//在文档中绘制颜色 ,startPos:开始位置点,endPos:结束位置点 void CodeEditer::drawColorInDocument(int startPos,int endPos) { if( curDrawColorPosition_.startPos == startPos && curDrawColorPosition_.endPos == endPos ) return ; // 取大值 checkPosition( curDrawColorPosition_.startPos,true ); checkPosition( curDrawColorPosition_.endPos,true ); // 取大值 checkPosition( startPos , true ); checkPosition( endPos , true ); // LOG_DEBUG("[CodeEditer::drawColorInDocument] curDrawColorPosition_.startPos = {},curDrawColorPosition_.endPos = {}",curDrawColorPosition_.startPos,curDrawColorPosition_.endPos); // LOG_DEBUG("[CodeEditer::drawColorInDocument] startPos = {},endPos = {}",startPos,endPos); //获取光标 QTextCursor cursor(this->document()); QTextCharFormat format; //关闭信号,防止合并格式后,重复发送修改文本的信号 this->blockSignals(true); QColor color; // 复位 //清空上一次 选中的内容 color.setRgb(135, 206, 250, 0); //完全透明=无色 format.setBackground(color); cursor.setPosition(curDrawColorPosition_.startPos, QTextCursor::MoveAnchor); cursor.setPosition(curDrawColorPosition_.endPos, QTextCursor::KeepAnchor); cursor.mergeCharFormat(format); cursor.clearSelection(); if( endPos || startPos ) { // 设置高亮 color.setRgb( 135, 206, 250, 51 ); //51/255=20透明度 format.setBackground( color ); cursor.setPosition( startPos, QTextCursor::MoveAnchor); cursor.setPosition( endPos , QTextCursor::KeepAnchor); cursor.mergeCharFormat(format); cursor.clearSelection(); } curDrawColorPosition_.startPos = startPos; curDrawColorPosition_.endPos = endPos; this->blockSignals(false); }//在文档中绘制颜色 ,startPos:开始位置点,endPos:结束位置点 void CodeEditer::drawColorInDocument(int startPos,int endPos) { if( curDrawColorPosition_.startPos == startPos && curDrawColorPosition_.endPos == endPos ) return ; // 取大值 checkPosition( curDrawColorPosition_.startPos,true ); checkPosition( curDrawColorPosition_.endPos,true ); // 取大值 checkPosition( startPos , true ); checkPosition( endPos , true ); // LOG_DEBUG("[CodeEditer::drawColorInDocument] curDrawColorPosition_.startPos = {},curDrawColorPosition_.endPos = {}",curDrawColorPosition_.startPos,curDrawColorPosition_.endPos); // LOG_DEBUG("[CodeEditer::drawColorInDocument] startPos = {},endPos = {}",startPos,endPos); //获取光标 QTextCursor cursor(this->document()); QTextCharFormat format; //关闭信号,防止合并格式后,重复发送修改文本的信号 this->blockSignals(true); QColor color; // 复位 //清空上一次 选中的内容 color.setRgb(135, 206, 250, 0); //完全透明=无色 format.setBackground(color); cursor.setPosition(curDrawColorPosition_.startPos, QTextCursor::MoveAnchor); cursor.setPosition(curDrawColorPosition_.endPos, QTextCursor::KeepAnchor); cursor.mergeCharFormat(format); cursor.clearSelection(); if( endPos || startPos ) { // 设置高亮 color.setRgb( 135, 206, 250, 51 ); //51/255=20透明度 format.setBackground( color ); cursor.setPosition( startPos, QTextCursor::MoveAnchor); cursor.setPosition( endPos , QTextCursor::KeepAnchor); cursor.mergeCharFormat(format); cursor.clearSelection(); } curDrawColorPosition_.startPos = startPos; curDrawColorPosition_.endPos = endPos; this->blockSignals(false); }
最新发布
10-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青鸟青史

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值