Solved How can I load big text file into QPlainTextEdit?
-
Hello, I wonder why I can't load 10MB of content into QPlainTextEdit.
Windows default editor, Notepad able to load 10MB of text file. but QPlainTextEdit unable to do it.
The code is something like this.QFile file(fileName); file.open(QFile::ReadOnly); QString content = QString::fromUtf8(file.readAll()); ui->plainTextEdit->setPlainText(content); file.close();
It stuck at setPlainText.
It never finish.
Plus, Windows task manger says the application uses 400MB of memory + 25% of cpu for loading 10MB of text file.
How can I load big text file into QPlainTextEdit?I also tried something like below, but no luck
ui->plainTextEdit->setReadOnly(true); ui->plainTextEdit->setUndoRedoEnabled(false); setUpdatesEnabled(false);
-
Hi,
first of all please tryfile.open(QIODevice::Text | QIODevice::ReadOnly);
Secondly, readAll(); does not perform any error checking. Should you still experience issues please try to loop the read with
content.append(file.readLine());
and place some error checking inside the loop.Btw. I just generated 10MB txt file of random characters - no issues reading that one with the code you provided.
From what your task mngr shows - you have some endless loop there.
-
Thank you very much for your reply.
I modified code so that it looks like this:QFile file(fileName); file.open(QIODevice::Text | QIODevice::ReadOnly); QString content; while(!file.atEnd()) content.append(file.readLine()); ui->plainTextEdit->setPlainText(content); file.close();
content.append works very well.
But ui->plainTextEdit->setPlainText(content);
never finish.
Can I have some more hint? -
I am sorry to hear you still have problems.
Other workaround that comes to my mind is to load the text into QTextDocument, set it to plain text and attach into text editor. -
@artwaw I just found that if I there exist newline characters, it loads well. But if there is no newline character, it never finish. I wonder if this is normal behavior.
-
Great to hear you've solved it.
I do not know if it is standard behavior however I always use QTextDocument together with editors. -
@artwaw I also noticed that if I disable wordwrap with
ui->plainTextEdit->setWordWrapMode(QTextOption::NoWrap);
it loads 10MB text file without newline well.
I think the problem was based on Qt's word wrap.