Using QPlainTextEdit as a log viewer
-
wrote on 8 Aug 2014, 06:24 last edited by
Hello,
I have a log viewer dialog which has a QPlainTextEdit to show the output of a process (which is going into a file). I open the file in the log viewer class (keep it open as long as the log viewer object lives) using QFile. I have a QTextStream and I set the QFile to this QTextStream. I then have timer for every 5 seconds and when that expires, I read until end of the stream, append the text I have read to the QPlainTextEdit, save the last read position from the stream and start the timer again.
The process logs a lot of messages into the file, as the number of messages grow, my log viewer hangs when trying to update the QPlainTextEdit.
Is my approach to display a log window wrong?
Can I redirect the output of the QProcess to the QPlainTextEdit? (I set the setStandardOutputFile and setStandardErrorFile)
In my approach is QTextStream unable to handle the inflow of log messages?Any help would be appreciated.
Regards,
Bharath -
wrote on 8 Aug 2014, 06:45 last edited by
@1) You should make sure that you really only add text and not always replace the whole contents which consistently grows.
@2) You could start the process keeping the process object and connect to its readyReadStandardOutput and readyReadStandardError signals and on each incoming signal append readAllStandardOutput()and readAllStandardError() to the logger.
-
wrote on 8 Aug 2014, 09:07 last edited by
bq. @1) You should make sure that you really only add text and not always replace the whole contents which consistently grows.
This is what I am doing. I append only the new content to the QPlainTextEdit, I don't replace the whole thing.
bq. @2) You could start the process keeping the process object and connect to its readyReadStandardOutput and readyReadStandardError signals and on each incoming signal append readAllStandardOutput()and readAllStandardError() to the logger.
I am going to try this now.
-
Do you incrementally read the contents of the file or reading all the contents of the file every time and dump entire content ? It is better if you keep the track incremental read and append only what is required.
-
wrote on 8 Aug 2014, 13:04 last edited by
bq. Do you incrementally read the contents of the file or reading all the contents of the file every time and dump entire content ? It is better if you keep the track incremental read and append only what is required.
What I had done initially is to open the file, assign the file to a text stream. I read until the end of the stream, record the position, close the file. After a time out, I opened the file again, seek to the recorded position in the stream, read from there until end. I know that this isn't the effective way as the amount of time taken to open file, seek to a given position in the stream would grow exponentially as the file size increases.
1/5