4

I have to use Qt show the context of a file, whose size is 70M, but is so slow that it takes several minutes to display.

QFile file("farey.txt");
    file.open(QFile::ReadOnly | QFile::Text);
    QTextStream ReadFile(&file);
    while (!ReadFile.atEnd()) {
        QString line = ReadFile.readLine();
        ui->Output->append(line);
    }
    file.close();

Output is TextEdit, can anyone give me some help to make it faster?

Can I use Qt to dispatch a default system editor to open the file??

Kyuvi
  • 360
  • 3
  • 13
tian tong
  • 793
  • 3
  • 10
  • 21

3 Answers3

3

First of all, this is enough:

QFile file("farey.txt");
file.open(QFile::ReadOnly | QFile::Text);
ui->Output->setPlaintText(file.readAll());

Second one - best optimization in your case it's logic optimization. Did you really need to show all of this 70M file?

RazrFalcon
  • 787
  • 7
  • 17
  • yes. My only difficulty is show file of size 70M. I use this but can't show it in 1 minutes. Can I use a system default editor to open file rather than show it in `TextEdit`? – tian tong Sep 28 '15 at 15:24
  • ?tian tong I think @RazrFalcon was hinting at the possibility of reading only part of the file at one time. Presumably you don't display all 70 Mbytes of it on the screen at one time. So - could you possibly only read in the 100K that is actually displayed. Before the availability of large memory chips, this is what word processors / text editors used to do. – Michael Vincent Sep 28 '15 at 16:28
  • If it doesn't have much delay, I can read part and part. But how can I do? when I use a loop to read line by line, It display all after a huge delay rather than display line by line – tian tong Sep 29 '15 at 04:03
  • @tian_tong you'll have to experiment with different file reading methods. There does not have to be a delay in reading from a file. Maybe the method you use to read, for example line 1000 reads all 999 lines before hand. I did this about 20 years ago in a DOS utility. Sadly, I have lost the code. – Michael Vincent Sep 29 '15 at 08:23
3

If you want to display your file as plain text, the widget QPlainTextEdit is better then QTextEdit. It is optimized to handle large documents, for example see QTextEdit vs QPlainTextEdit

QTextEdit is an advanced WYSIWYG viewer/editor supporting rich text formatting using HTML-style tags.

QTextEdit can display images, lists and tables.

QPlainTextEdit is an advanced viewer/editor supporting plain text.

QPlainText uses very much the same technology and concepts as QTextEdit, but is optimized for plain text handling.


It is possible to open a file by default system file handler using QDesktopServices, for example:

QDesktopServices::openUrl(QUrl::fromLocalFile("file_path"));
Community
  • 1
  • 1
Orest Hera
  • 6,706
  • 2
  • 21
  • 35
1

Your problem is likely the appending the contents of the file line-by-line. This forces the document to be laid out repeatedly on each line - that's the cause of the slowdown. Read the entire file in one go, and set it on the editor using setPlainText.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • I also can't show it in 1 minutes. Is there any other choice? I can use any other object, the only request is show it quickly. For example, can I dispatch a system default editor to show the file? – tian tong Sep 28 '15 at 15:16