Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: How to open Html file as plain text?

Tags:

c++

html

file

text

qt

I'm writing a basic text editor, where I want to edit HTML-files. Currently I have a QTextEdit where I can write text, then save to file/open from file.

The problem is that when I open a HTML file, it doesn't open as plain text. Rather, it opens as processed HTML. This happens even if I save as .txt. So I can write

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

in the textEdit, save it as a text file. But if I open it, suddenly it's processed HTML. Same thing happens when I open Html files that are saved from Notepad++.

How can I open the file as plain text, just like notepad does?

Heres the code I have as of now:

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();
    }
}
like image 412
Einar Avatar asked Dec 11 '14 02:12

Einar


1 Answers

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

Changing my code from

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

to

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

and it opens everything i text form, and not processed HTML.

like image 153
Einar Avatar answered Oct 23 '22 15:10

Einar