Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Update QTextEdit content with changes of a big trace file.
Forum Updated to NodeBB v4.3 + New Features

[Solved] Update QTextEdit content with changes of a big trace file.

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fracu
    wrote on last edited by
    #1

    Hi there!

    I'm trying to present the contents of a trace file into a QTextEdit, since this file is changing constantly I need to implement a mechanism to regularly update the QTextEdit with the changes. The trace file is a text file that has a pretty big size, it may grow up to 110MB when it is rolled and size comes back to 0.

    So far what I'm doing is calling a function (using a QTimer) that checks the trace file size:
    @
    void MainWindow::checkTraceFileSize() {
    qint64 Size = QFile("trace.dat").size();
    if(Size!=TraceFileSize) traceFileChanged();
    TraceFileSize=Size;
    }
    @

    If the trace file has changed it's size traceFileChanged is called:
    @
    void MainWindow::traceFileChanged() {
    long nLine=0;
    QFile TraceFile("trace.dat");
    if (TraceFile.open(QIODevice::ReadOnly)) {
    QTextStream in(&TraceFile);
    while(!in.atEnd()) {
    if(nLine>=TraceFileLine) { //New Lines since last check...
    processLine(in.readLine()); //Add the line to the QTextEdit
    TraceFileLine++;
    }
    else in.readLine();
    nLine++;
    }
    }
    TraceFile.close();
    }
    @

    Basically if the size has changed I read the big file line after line discarding the ones that had already been written to the QTextEdit and adding the new ones to it.

    BUT since the trace file is so big the app freezes for one or two seconds each time new lines are added to the file.

    To put it simply I need to implement some kind of "tail -f" to a QTextEdit of a big file.

    Is there a better way to achieve this?

    Thanks in advance!

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      You already have the previous file size available in <code>TraceFileSize</code>, so you may "seek":http://doc.qt.digia.com/qt/qiodevice.html#seek to the position where you've left off on your last iteration, so you only have to process the new content which has been added to the file.

      If this doesn't help you may processEvents() within your loop (be aware that your timer might elapse in between and generate another event); the whole operation also qualifies for beeing executed in a secondary thread (as long as you use a queued signal / invokeMethod() to update the QTextEdit).

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Fracu
        wrote on last edited by
        #3

        Thank you Lukas! That was helpful.

        Now instead of counting lines I'm saving the position inside the file since last read (in traceFilePos), then I use this pos to seek in the file:

        @
        void MainWindow::traceFileChanged()
        {
        QFile traceFile("trace.dat");
        if (traceFile.open(QIODevice::ReadOnly))
        {
        QTextStream in(&traceFile);
        in.seek(traceFilePos);
        while(!in.atEnd())
        {
        QString line = in.readLine();
        processLine(line);
        traceFilePos+=line.length();
        }
        }
        traceFile.close();
        }
        @

        Now it works smooth...
        Thank you again!

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          You're welcome!

          Feel free to prepend the initial post title with '[Solved] ...' to indicate that there is a solution inside.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved