Text in QTextEdit is distorted

720 views Asked by At

I have a QTextEdit in which I want to set some text (unicode) that has been generated by an ASCII Art text renderer. Everything works fine, except that the QTextEdit is distorting the text. The screenshot shows the same unicode text variable set in the QTextEdit via setText() and logged to the console, where it is displayed correctly.


QTextEdit distorts text


It doesn't seem to matter if I use QTextEdit or QPlainTextEdit, or if I am setting the text via setText(ascii_art_text) or setPlainText(ascii_art_text). I'm using PySide here, however I suspect that isn't of too much interest in this case.


Any ideas how to fix that distortion?

1

There are 1 answers

0
Uyghur Lives Matter On BEST ANSWER

From the looks of your example, it appears that your QTextEdit is not using a monospaced font which is what terminals and text editors typically use. Create a QFont using a monospaced font. E.g.,

# Specific Windows monospaced font.
font = QFont("Courier New")

According to Torsten Marek's answer in How to specify monospace fonts for cross platform Qt applications?, you can get a cross platform monospaced font with:

font = QFont("Monospace")
font.setStyleHint(QFont.TypeWriter)

Once you've determined your font, assign it to your QTextEdit:

text_edit.setCurrentFont(font)