QTextEdit with different text colors (Qt / C++)

后端 未结 6 850
Happy的楠姐
Happy的楠姐 2020-12-05 18:00

I have a QTextEdit box that displays text, and I\'d like to be able to set the text color for different lines of text in the same QTextEdit box. (i

6条回答
  •  鱼传尺愫
    2020-12-05 18:25

    The ONLY thing that worked for me was html.

    Code snippet follows.

    QString line = "contains some text from somewhere ..."
        :
        :
    QTextCursor cursor = ui->messages->textCursor();
    QString alertHtml = "";
    QString notifyHtml = "";
    QString infoHtml = "";
    QString endHtml = "
    "; switch(level) { case msg_alert: line = alertHtml % line; break; case msg_notify: line = notifyHtml % line; break; case msg_info: line = infoHtml % line; break; default: line = infoHtml % line; break; } line = line % endHtml; ui->messages->insertHtml(line); cursor.movePosition(QTextCursor::End); ui->messages->setTextCursor(cursor);

提交回复
热议问题