I
I
Ivan2017-03-06 08:43:20
Python
Ivan, 2017-03-06 08:43:20

Why can't I open html file in textedit pyqt?

Actually, I want to open an html file and put its text in QTextEdit (PyQT)
I write the following construction:

def showDialog(self):
        filename = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', '/')
        file=open(filename[0])
        data = file.read()
        self.ui.textedit.setPlainText(data)

When you try to open an html file, the program crashes and closes
. Not html files open normally.
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan, 2017-03-06
@iwqn

Found the reason
f = open(filename[0],"rb")
data = f.read() data = data.decode
('utf-8',errors='ignore')
the reason was not in QTextEdit

R
rhaport, 2017-03-06
@rhaport

For HTML there is a setHtml function.
You can also try doing QTextEdit::toPlainText() first

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question