QTextCursor::setPosition: Position ‘ ‘ out of range

17 篇文章 0 订阅

使用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() + strText.length(),QTextCursor::KeepAnchor);//待删除
    cursor.select(QTextCursor::BlockUnderCursor);//选择光标下的文本块,也可改成只选中单词,待删除

    QTextCharFormat fmt;
   //根据不同等级绘制不同颜色
//   QPainter p(exportMessage);
    switch (level)
    {
    case Critical_INFO:
        fmt.setForeground(Qt::red);
        break;
    case Warning_INFO:
        fmt.setForeground(QColor(255 ,193 ,37));//黄色
        break;
    case Running_INFO:
        fmt.setForeground(Qt::green);
        break;
    case Information_INFO:
        fmt.setForeground(Qt::black);
        break;
    default:
        break;
    }
    cursor.mergeCharFormat(fmt);
    cursor.clearSelection(); //撤销选中
    cursor.movePosition(QTextCursor::EndOfBlock);  //cursor和anchor均移至末尾
}

解决办法:把setPosition函数改用movePosition函数,以下是函数更改后的全貌:

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

    exportMessage->append(strText);

    //选中需要显示颜色的文字
    QTextCursor cursor = exportMessage->textCursor();
    cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor,1);//新增
    cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor,1);//新增
    QTextCharFormat fmt;
   //根据不同等级绘制不同颜色
//   QPainter p(exportMessage);
    switch (level)
    {
    case Critical_INFO:
        fmt.setForeground(Qt::red);
        break;
    case Warning_INFO:
        fmt.setForeground(QColor(255 ,193 ,37));//黄色
        break;
    case Running_INFO:
        fmt.setForeground(Qt::green);
        break;
    case Information_INFO:
        fmt.setForeground(Qt::black);
        break;
    default:
        break;
    }
    cursor.mergeCharFormat(fmt);
    cursor.clearSelection(); //撤销选中
    cursor.movePosition(QTextCursor::EndOfBlock);  //cursor和anchor均移至末尾
}

程序效果图:
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

青鸟青史

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

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

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

打赏作者

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

抵扣说明:

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

余额充值