Slow performances with QPlainTextEdit
Hello All,
I'm trying to make a custom TextEditor with some cool features, but I have a show stopper here its the perf of this widget...
I'm drag and dropping a 200Kb text file, and it takes approx 5s to load, where it's near to instantly on most of other standard text editor...
I'm filling the widget like this:
Code:
{
// Unpack dropped data and handle it the way you want
// check for our needed mime type, here a file or a list of files
if (mimeData->hasUrls())
{
QList<QUrl> urlList = mimeData->urls();
// extract the local paths of the files
for (int i = 0; i < urlList.size(); ++i)
{
QFile file(urlList.
at(i
).
toLocalFile());
return;
//QString line;
setIsUpdating();
while( !stream.atEnd() ) {
//line = stream.readLine();
this->appendPlainText(stream.readLine());
}
file.close(); // when your done.
setIsUpdating(false);
}
}
}
I have also customized a few things, but I therefore disabling all painting events while i'm filling the text, with the setIsUpdating function.
I also even disabled all the fancy stuff to only have a QPlainTextEdit sublass with the drag and drop event and I still have this slowness.
Full code of the editor can be seen here: http://pastebin.com/1jGtFTX8
I don't really know why is it so slow :/
Any ideas ?
Re: Slow performances with QPlainTextEdit
You might try using QTextStream::readAll(). Heed the warning though.
On my machine loading a 362 KB file from disk into a QPlainTextEdit took just over 1 second using readLine() and 40 ms using readAll().
Re: Slow performances with QPlainTextEdit
And instead of using setIsUpdating() you could simply use QObject::blockSignals(). That has nothing to do with your original problem, just a side note.
Re: Slow performances with QPlainTextEdit
I'm doing like this now:
this->appendPlainText(stream.readAll());
and it works way better :)
Also, for the blockSignals, this is good to now, but I think I'll remove the isEditing function... It's been years the last time I've made C++/Qt now, so I kind of forgot the few I knew
Thanks dudes !
Re: Slow performances with QPlainTextEdit
Why are you appending line by line instead of just using setPlainText()?
Re: Slow performances with QPlainTextEdit
Good Question ;) I changed that, but I don't think this were causing trouble to append the whole doc instead of setting it... As i'm just testing for now ;)