PDA

View Full Version : QTextEdit colored text, again...



nsvinc
29th December 2015, 13:00
Hello,

I'm looking for a solution to append single characters to QTextEdit with different colors.
I tried the following:



QTextCursor curs(textCursor());
QTextCharFormat fmt;
fmt.setForeground(QBrush(Qt::red));
curs.setCharFormat(fmt);
curs.movePosition(QTextCursor::End);
curs.insertText(QString(chr));

does not work



setTextColor(kolor1);
append(QString(chr));

does not work either

The only thing that actually works, is the <font> tag


append(QString("<font color=red>%1</font>").arg(chr));


The problem is, I'm getting the input char by char, and cannot combine it into QString easily; every new character could have a different color. I would like to find a way other than tagging every single character (which would yield tons of garbage text not seen by the user, and would be probably not speed efficient).

It would be nice, if QTextEdit remembers the last color setting, and appends new characters with that color...

code_err
29th December 2015, 18:00
As far as I know it's not possible. You can either set background/text color for entire QTextEdit or use html as You have done. It is possible to create function that will wrap Your char into html and set random color and function that will take back only char from html. The second is easy. You can take entire text from QTextEdit, put it in QTextDocument object and use function toPlainText(), then you have Your text as QString without any html.