I created a special file viewer tool in Qt to display text data around 1-30 MBytes.
I used QPlainTextEdit
in readonly mode with disabled word wrapping to display the text.
I have a 20MByte example file and the call of QPlainTextEdit::setPlainText()
takes about 6.5 seconds which is not acceptable by me wanting to use this tool very often.
Opening the same file with Notepad++ or TotalCommander's Lister takes an imperceptible amount of time (much less than a second).
Do you have any idea how can I solve this?
Can it be improved with this editor type?
Is there any other text viewer class that can do it faster?
Is it possible to create an own text viewer class to improve performance?
QPlainTextEdit is slow. It was designed to show not very big chunks of text, not more than 1M lines very roughly or even less depending on your platform.
The known solution is model based. Use a fast C++ model and something like a QListView (or QML ListView) and show only the visible lines. Update the view everytime you receive events from sliders, wheel, keyboard. Just like QTableView works. Or you can draw the view also manually with a QPainter if you need colors and some special formatting, like line-numbers or icons.
It is easy to create a simple readonly log-view like widget. Creating an editor is a huge task, because you'll have to deal with selection, deletion, copy-pasting and what not manually.
I am not sure how helpful my answer will be (most of it is based on personal experience).
Can it be improved with this editor type?
You can not improve performance of setPlainText()
, but you can try to improve your highlighting mechanism which will result in better performance.
QPlainTextEdit::setPlainText()
is a simple interface to set plain text in your editor but behind the scenes it does some other things such as Syntax highlighting which can noticeably reduce performance because highlightBlock()
will be called for every block of text in your file. So if you have 1 million lines in your text file, there will 1 million calls to highlightBlock()
. This impacts performance and there is no way to get around this 'easily'.
Most editors I have seen use regular expressions to parse the current text block and then highlight it. A first step towards better performance could be to replace the regular expressions with manual parsing. I have tried this multiple times and it always results in better performance.
One other way, which is rather uncommon, is to use multi-threading for highlighting. You can get all the text in the file and send it to a 'highlighter worker' and let it do the highlighting in the background. You can see this in action here and here.
Is there any other text viewer class that can do it faster?
You can try Scintilla, which is what Notepad++ uses. It can be integerated with Qt C++ and works quite well. There are a few Qt apps that are using it, one example is textosaurus.
Is it possible to create an own text viewer class to improve performance?
Yes, but that will be a monumental task. Read @Vasilij's answer above for this.
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