4

I'm building an app that will read in a gcode file. Files range from a few KB to a few hundred MB (but that's rare). After a user selects a file, the program goes through it and reads it line by line. This doesn't take long at all.

I want to display the text of the file I just read in in a QTextEdit widget. Unfortuantely, doing this takes a really long time (a few seconds for even small files).

I've thought about just adding a "loading, please wait" message for the user, but I was wondering if there was a way to speed up the text loading process.

right now my code looks like this:

 QTextStream in(gcodeFile); //The file path was given by the user
 ui->textEdit->setText(in.readAll());
 in.seek(0);
 processGcode();

Is there a better way to load a lot of text into a QTextEdit that won't make the app hang?

Note: The issue is not that I'm reading through the file twice. I changed my code so that it did the processing at the same time as adding the text to the QTextEdit, but the whole thing still took too long.

Memnarch1113
  • 99
  • 1
  • 8
  • Have you tried using `QPlainTextEdit`? It is a more slicker widget for displaying (and editing) text that doesn't come with a lot of what `QTextEdit` carries around (see [here](http://stackoverflow.com/a/17466240/1559401)). As the description states `It is optimized to handle large documents and to respond quickly to user input.` which seems to be your case. – rbaleksandar Sep 08 '16 at 18:42
  • Oh wow! Thanks a lot, this is perfect. If you post this as an answer I'll mark it as correct. – Memnarch1113 Sep 08 '16 at 19:10
  • 1
    Don't say hurrah just yet before trying it out. `QPlainTextEdit` isn't all that much faster than `QTextEdit` in most scenarios. See what improvement, if any, you're actually getting. – Kuba hasn't forgotten Monica Sep 08 '16 at 19:46
  • Is the G-code meant to be editable? – Kuba hasn't forgotten Monica Sep 08 '16 at 21:17
  • I did try it and it works well. The G-code could be editable, I haven't decided yet – Memnarch1113 Sep 08 '16 at 22:06
  • So is `QPlainTextEdit` is the solution or not? :D @KubaOber I have actually noticed a considerable gain once when I switched to it - approx. factor of 3 but then again I might have done something else to get this boost. – rbaleksandar Sep 09 '16 at 03:23
  • It is a solution iff it works for you :) If you want another solution, QScintilla is worth investigating, too. – Kuba hasn't forgotten Monica Sep 09 '16 at 12:37

1 Answers1

2

"QPlainTextEdit is an advanced viewer/editor supporting plain text. It is optimized to handle large documents and to respond quickly to user input."

You can see check detailed information from here.

Wilmort
  • 294
  • 2
  • 15
  • "It is optimized to handle large documents..." according to my experience this is utter nonsense. It is not optimized at all. It requires the whole text to be loaded into memory and opening larger files (e.g. 100000 of rows or more) is a nightmare. – HiFile.app - best file manager Mar 21 '22 at 09:09