Qt: How to open an HTML file as plain text?

I am writing a main text editor where I want to edit HTML files. I currently have a QTextEdit where I can write text and then save to a file / open from a file.

The problem is that when I open the HTML file, it does not open like plain text. Rather, it opens as processed HTML. This happens even if I save as .txt. Therefore i can write

<html> <h1>Test</h1> </html> 

in a text element, save it as a text file. But if I open it, it suddenly processes HTML. The same thing happens when I open Html files that are saved from Notepad ++.

How can I open a file in plain text, how does notepad do it?

Here is the code that I have at the moment:

 void Notepad::on_actionOpen_triggered() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open file"), QString(), tr("Text Files (*.txt);;C++ Files (*.cpp *h);;All types (*.*)")); if (!fileName.isEmpty()) { QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { QMessageBox::critical(this, tr("Error"), tr("Could not open file")); return; } QTextStream in(&file); ui->textEdit->setText(in.readAll()); file.close(); } } 
+5
source share
1 answer

I finally found out. There is a function called setPlainText ()

Change my code from

 ui->textEdit->setText(in.readAll()); 

to

 ui->textEdit->setPlainText(in.readAll()); 

and it opens all text forms, but does not process HTML.

+5
source

Source: https://habr.com/ru/post/1208776/


All Articles