I have an escape sequences in a text, like
char const * text = "\e[1;33m" "some colored text" "\e[0m";
Which usually printed in terminal. But I want to forward this text in QTextEdit.
What is the simpliest way to make such text in QTextEdit colored? How to perform a filtering of escape sequences, i.e. how replace its with proper text formatting?
I think there is QSyntaxHighlighter-aided solution possible, but I don't know what to do with escape sequences itself and how to hide they from the output.
Those are ANSI escape codes. They are very simple to parse, especially if you have just a small fixed subset you control. There probably are libraries which could also help you parse the escape codes, and if you see a need to support more codes than just colors, then it might make sense to try and find one, but if you just need simple color codes and have full control of the original strings, then writing your own is probably quicker.
So what you need to do, is parse those escape codes from the strings, and replace them with Qt rich text markup, which is a subset of HTML markup.
Another approach might be to do the reverse: store your strings using HTML (or some other) markup, then convert that to ANSI codes for colored console output. With luck you might even find a library which can convert HTML to text with ANSI escape codes.
Finally, I found the approach (it is understood, that
QTextEdit::setReadOnly(true)
):It is not fully tested at the moment, but quite workable. It is exact what I wanted.