QTextEdit設置當前行的文本的顏色和字體

思路

設置當前行文本的格式 需要使用到QTextCharFormat類,設置它的文本顏色或者文本字體,然後並獲取 當前文本的光標,從光標開始位置設置文本的字符格式。

代碼

1、設置文本當前行的顏色

void MainWindow::setInsertTextColor(const QColor &color)

{
    QTextCharFormat fmt;//文本字符格式
    fmt.setForeground(color);// 前景色(即字體色)設爲color色
    QTextCursor cursor = ui->infoTextEdit->textCursor();//獲取文本光標
    cursor.mergeCharFormat(fmt);//光標後的文字就用該格式顯示
    ui->infoTextEdit->mergeCurrentCharFormat(fmt);//textEdit使用當前的字符格式
}

2、設置文本當前行的字體

 

void MainWindow::setInsertTextFont(const QFont &font)
{
    QTextCharFormat fmt;//文本字符格式
    fmt.setFont(font);//字體
    QTextCursor cursor = ui->m_textdisplay->textCursor();//獲取文本光標
    cursor.mergeCharFormat(fmt);//光標後的文字就用該格式顯示
    ui->m_textdisplay->mergeCurrentCharFormat(fmt);//textEdit使用當前的字符格式
}

3、使用總結

//設置時間顏色與字體
    setInsertTextFont(timeFont);
    setInsertTextColor(timeColor);
    ui->infoText->append(curTime.toString());

 

發佈了16 篇原創文章 · 獲贊 22 · 訪問量 2321
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章