美文网首页
QT:设置qtextedit文本框中某个字符的格式

QT:设置qtextedit文本框中某个字符的格式

作者: Machin_Yin | 来源:发表于2017-12-27 16:31 被阅读0次

    原文出处:http://blog.sina.com.cn/s/blog_a07a3f180102uyou.html

    void Widget::setCharColor(unsigned int pos)

    {

    if(pos <= 0)return ;

    QTextCursor cursor = ui->view1->textCursor();

    cursor.movePosition( QTextCursor::StartOfLine );//行首

    cursor.movePosition( QTextCursor::Right, QTextCursor::MoveAnchor, pos-1);//向右移动到Pos

    cursor.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor );

    ui->view1->setTextCursor( cursor ); // added

    QTextCharFormat defcharfmt = ui->view1->currentCharFormat();

    QTextCharFormat newcharfmt = defcharfmt;

    newcharfmt.setFontUnderline( true );

    newcharfmt.setUnderlineColor( QColor( Qt::red ) );

    newcharfmt.setUnderlineStyle( QTextCharFormat::SingleUnderline );

    ui->view1->setCurrentCharFormat( newcharfmt );

    cursor.movePosition( QTextCursor::PreviousCharacter );//加上这句是为了去除光标selected

    ui->view1->setTextCursor( cursor ); // added

    // ui->view1->setCurrentCharFormat( defcharfmt );

    ui->view1->setFocus();

    }

    相关文章

      网友评论

          本文标题:QT:设置qtextedit文本框中某个字符的格式

          本文链接:https://www.haomeiwen.com/subject/jqbxlxtx.html