ANSI Codes are not working on a QTextEdit

1

So I'm developing a project in Python and I'm using PyQt5. In the project I have a QTextEdit where I need to put some words in different colors.

I'm using ANSI encoding but it's not working, I already tried other modules like colorama and colors but none worked, can anyone help me please?

My operating system is Windows.

What appears in QTextEdit :

    
asked by anonymous 27.12.2015 / 01:01

1 answer

0

If you are in a 'warrant' field, stick to the specifications. If it is determined to be "ANSI" colors - these are relative to the use of the terminal - your project probably must have a terminal output, not a graph window drawn by Qt.

Qt is a very high-level framework that, among other things, allows you to create graphical programs with rich-text, and to format text, uses HTML and CSS conventions. ANSI codes are a breaker so that terminals can display colored text.

You have to decide: if you have freedom in your project to do in Qt rather than terminal, you also naturally have the freedom to color the text in ways other than ANSI code. If you do not have this freedom (for example, if it is a school work), you certainly can not change the terminal design to a Qt program. (If for no other reason, precisely why in Qt ANSI codes do not work and has any meaning).

Note that in addition to colors that can be a help in viewing there are other factors that could require the output of the program to be in the terminal: in the terminal, by default, the standard output can be redirected to one file or as input from another program - and in those cases, it is important that your Qt project be compatible with that.

(Remember 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")
    
28.12.2015 / 13:42