How in QTextEdit to make your color for each line? That is, not the entire widget, but specifically for the string. For example, I want green. I do this:

 ui->textEdit->SetStyleSheet("Тут задаем цвет"); 

... but the color changes for the entire widget at once and all the lines in it. And I need only ui->textEdit->append("Строка") be green, and everything after it - in standard black.

  • Multicolored text can be done 1.RichEdit (easiest). 2. Set the property "OwnerDraw" = true and draw through the OnPaint event "manually" as you like. - nick_n_a
  • How to paint RichEdit text is here , and from to can be calculated by the length of the lines. - nick_n_a

1 answer 1

You can, for example, like this:

 QTextEdit txt_edit; QTextCharFormat fmt = txt_edit.currentCharFormat(); fmt.setForeground(QBrush(Qt::red)); txt_edit.setCurrentCharFormat(fmat); txt_edit.append("tra-ta-ta"); fmt = txt_edit.currentCharFormat(); fmt.clearForeground(); txt_edit.setCurrentCharFormat(fmat); 

It can be different:

 QTextEdit txt_edit; txt_edit.insertHtml("<div><font color=\"red\">tra-ta-ta</font></div>");