Important: Please read the Qt Code of Conduct -

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

  • 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();

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

    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!

  • You already have the previous file size available in <code>TraceFileSize</code>, so you may "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).

  • 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 (
    QTextStream in(&traceFile);;
    QString line = in.readLine();

    Now it works smooth...
    Thank you again!

  • You're welcome!

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

Log in to reply