If you’re in a dirty warrant, stick to the specs. If it is determined to be "ANSI" colors - these are related to terminal use - your provavelemtne project has to have a terminal output, not a graphics window designed by Qt.
Qt is a very high-level framework, which among other things allows the creation of graphics programs with rich-text, and to format text, uses HTML and CSS conventions. ANSI codes are a puzzle so terminals can display colored text.
You have to decide: whether you have the freedom in your project to do it in Qt instead of terminal , you also naturally have the freedom to color the text in ways other than ANSI code. If you don’t have that freedom (for example, if it’s a school project), you certainly can’t change the terminal project to a Qt program. (If for no other reason, precisely why ANSI codes do not work and have no meaning in Qt).
Note that in addition to the colors that can be an auxiliary in the visualization there are other factors that could require the output of the program to be in the terminal: in the terminal, by default, the default output can be redirected to a file or as input from another program - and in such cases, it is important that your Qt project is compatible with this.
(Remembering that you can use the inline "style" tag attribute to put the color you want in your text inside a Qtextwidget:
import sys
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
w.show()
t = QtGui.QTextEdit('', w)
t.show()
t.append("teste")
t.append("<b>negrito</b>b")
t.append("<b style='color:red'>negrito vermelho</b>b")
And what would be the reason to use ANSI instead of HTML? Unless I am mistaken, there is nothing mentioning ANSI control characters in the Qt documentation. HTML, on the other hand, even has its own method for using HTML:
setHtml()
More details here: http://doc.qt.io/qt-5/richtext-html-subset.html– Bacco
In the project I was told to do with ANSI, I don’t know why they don’t want HTML. One thing I realized is that if you use ANSI encoding, characters like ' n' for example are no longer recognized. In addition this Qtextedit will display information coming from a serial port.
– user36793
You’ll have to write a small converter then.
– Bacco
As @Bacco said, you will have to write a converter, using the color table among others and apply with
setFontWeight
(weight/bold or not),setFontItalic
,setUnderlineStyle
,QTextFormat::setForeground
... will have to adjust one by one. Because nativelyQTextEdit
doesn’t work with that.– Guilherme Nascimento