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.e. line 1 might be red, line 2 might be black, etc.)
Is this possible in a QTextEdit
box? If not, what's the easiest way to get this behavior?
Thanks.
Link to doc
A few quotes:
.
.
This means mostly deprecated tags and as such does not include any current CSS, so I turned to this:
Just a quick addition: an alternative to generating the html yourself, if you're populating the text box programatically, is to use
textEdit->setTextColor(QColor&)
. You can create the QColor object yourself, or use one of the predefined colours in the Qt namespace (Qt::black, Qt::red, etc). It will apply the specified colour to any text you add, until it is called again with a different one.This is my solution for a very simple error logging using QTextEdit.
This is how it looks like:
Of course, you can go ahead and add date/time and other cool stuff :)
Use text formated as HTML, for example:
where text, is a HTML formated text, contains with colored lines and etc.
Extending on https://stackoverflow.com/a/13287446/1619432:
QTextEdit::append()
inserts a new paragraph with the previously set FontWeight / TextColor.insertHTML()
orInsertPlainText()
to avoid inserting a new paragraph (e.g. to achieve different formats in a single line) do not respect the font/color settings.Instead use QTextCursor:
The ONLY thing that worked for me was html.
Code snippet follows.