ANSI codes are not working on a Qtextedit

Asked

Viewed 342 times

2

So I’m developing a Python project and I’m using Pyqt5. In the project there is a QTextEdit where I need to put some words in different colors.

I’m using ANSI encoding but it’s not working, I’ve tried other modules like Color and Color but none worked, can anyone help me please ?

My operating system is Windows.

What appears in the QTextEdit:

  • 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

  • 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.

  • You’ll have to write a small converter then.

  • 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 natively QTextEdit doesn’t work with that.

1 answer

0

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")
  • Thanks jsbueno, I talked to the staff and they decided to use HTML tags. I just wanted to ask you a question (just curious). The only way to color text in a Qtextedit is by using HTML ?

  • Yes. Why more of a way,q when html with inline style - or even separate, is much more complete and flexible? For example: ANSI codes have only 8 possible colors - with an arbitrary code for each color when used as front color, and another arbitrary code when used as background color. With Python’s HTML color notation and strigns handling facility, you can make a degrade word with a line of code if you want:

  • texto = u"HTML é muito mais flexível para estilos de texto" t.append(u''.join(u'<span style="color:#{:02x}{:02x}{:02x}">{}</span>'.format(int(255.0 * i /len(texto)) ,0, int(255 - 255.0 * i/len(texto)), letra) for i, letra in enumerate(texto) ))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.