3

I'm trying to print debug messages in QTextEdit widget. It's printing fine, but it doesn't decode some of the ANSI escape codes.

For example, to print a warning message, you can do this:

print("\033[43mWARNING MESSAGE\033[0m")

enter image description here

And I'm trying to do the same with QTextEdit:

textEdit = QTextEdit(self)
textEdit.append(u'\033[43mWARNING MESSAGE\033[0m')

but it doesn't work. The output is:

enter image description here

So do you know how to solve it?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Dmitry
  • 536
  • 6
  • 10
  • Have you tried : toto = unicode("\033[43mWARNING MESSAGE\033[0m") and then textEdit.append(toto) – Destrif Jul 01 '16 at 14:32
  • I think it's the same as textEdit.append(u"\033[43mWARNING MESSAGE\033[0m") and it doesn't work. I'm using python 3.5 and pyqt5 and there's no uncode() function – Dmitry Jul 01 '16 at 14:35
  • 2
    Those codes aren't "Unicode". They are ANSI codes and depend on the shell you are running. They certainly aren't portable into generic text display systems and I don't think they'll even work in Windows. – strubbly Jul 01 '16 at 14:40
  • I think you should be able to easily translate from the techniques used in the duplicate I've just pointed out. – strubbly Jul 01 '16 at 14:42
  • http://doc.qt.io/qt-5/qtextedit.html#setCurrentCharFormat – Destrif Jul 01 '16 at 14:44
  • Incidentally it turns out Windows *does* support ANSI codes at last, but only from Windows 10 TH2!! – strubbly Jul 01 '16 at 14:45
  • @strubbly thanks for explanation and yes it windows 10. Yes textEdit.setHtml is printing with colours but my problem is a bit different, because I'm trying to duplicate print() output into textEdit. – Dmitry Jul 01 '16 at 15:24
  • 2
    Ok then [this](http://stackoverflow.com/questions/26500429/qtextedit-and-colored-bash-like-output-emulation) is the exact duplicate of what you want. No text formatting in QT will read ANSI codes - you'll need to write your own. – strubbly Jul 01 '16 at 15:32

0 Answers0