PDA

View Full Version : Slow performances with QPlainTextEdit



nekkro-kvlt
17th February 2012, 21:34
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:

void logEditor::dropEvent(QDropEvent *de)
{
// Unpack dropped data and handle it the way you want
const QMimeData* mimeData = de->mimeData();

// 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());
if (!file.open (QIODevice::ReadOnly))
return;

QTextStream stream ( &file );
//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 ?

norobro
17th February 2012, 22:40
You might try using QTextStream::readAll() (http://doc.qt.nokia.com/4.7-snapshot/qtextstream.html#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().

Lykurg
17th February 2012, 22:55
And instead of using setIsUpdating() you could simply use QObject::blockSignals(). That has nothing to do with your original problem, just a side note.

nekkro-kvlt
18th February 2012, 00:11
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 !

wysota
18th February 2012, 14:47
Why are you appending line by line instead of just using setPlainText()?

nekkro-kvlt
18th February 2012, 14:59
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 ;)