pyqt5 QTextEdit Windows 10 ASCII symbols

369 views Asked by At

I use PyQt5 and QTextEdit widget. I want to get a plain text from file and show it in QTextEdit. This is what I have in a text file

enter image description here

This is how I initialize QTextEdit

self.info_text_edit.setPalette(palette)
font = QtGui.QFont()
font.setFamily("Courier")
font.setPointSize(10)
font.setStyleStrategy(QtGui.QFont.NoAntialias)
self.info_text_edit.setFont(font)
self.info_text_edit.setLineWrapMode(QtWidgets.QTextEdit.WidgetWidth)
self.info_text_edit.setReadOnly(True)

Then for append a text I just do

self.info_text_edit.append(str(msg))

With this approach on Linux - everything works well. But this is what I see as a result on Windows

enter image description here

1

There are 1 answers

0
Anton On

There was mistake in coding\decoding..

When I write msg to the file I should do following:

import platform
if platform.system() == "Linux":
    msg = str(msg)
elif platform.system() == "Windows":
    msg = str(msg).encode("utf-8").decode('cp1251')

Then when I read msg from file I should do this:

import platform
if platform.system() == "Linux":
    self.info_text_edit.append(str(msg))
elif platform.system() == "Windows":
    self.info_text_edit.append(str(msg).encode('cp1251').decode('utf-8'))