QT:設置qtextedit文本框中某個字符的格式

原文出處: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();

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。