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
Extending on https://stackoverflow.com/a/13287446/1619432:
QTextEdit::append()
inserts a new paragraph with the previously set FontWeight / TextColor.
insertHTML()
or InsertPlainText()
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:
...
// textEdit->moveCursor( QTextCursor::End );
QTextCursor cursor( textEdit->textCursor() );
QTextCharFormat format;
format.setFontWeight( QFont::DemiBold );
format.setForeground( QBrush( QColor( "black" ) ) );
cursor.setCharFormat( format );
cursor.insertText( "Hello world!" );
...