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();
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With