I have a log file that gets updated while the job is running. I want the text content displayed in the text browser, and that should be updated dynamically.
.h file
public:
void fillloginfo();
void initializeQTimer();
void closeEvent(QCloseEvent *event);
void fillLogInfoChronically(const QString& logFilePath);
private:
QTimer* m_Timer;
QString m_logFilePath;
std::ifstream m_logFileStream;
public slots:
void fillLogInfoChronicallySlot();
.cpp file
void logdialog::initializeQTimer(){
m_Timer = NULL;
//create the timer object
m_Timer = new QTimer(this);
QObject::connect(m_Timer,SIGNAL(timeout()), this,SLOT(fillLogInfoChronicallySlot()));
}
void logdialog::closeEvent(QCloseEvent *event)
{
m_Timer->stop();
if ( m_logFileStream.is_open()){
m_logFileStream.close();
}
}
void logdialog::fillLogInfoChronically(const QString &logFilePath)
{
uilog->textBrowser->clear();
m_LastLinePos = 0;
m_logFilePath = logFilePath;
std::string m_logFilePathStr= m_logFilePath.toStdString();
m_logFileStream.open(m_logFilePathStr.c_str());
if (m_logFileStream.is_open()){
fillloginfo();
m_Timer->start(1000);
}
}
void logdialog::fillloginfo()
{
std::string line;
while (getline(m_logFileStream,line)){
uilog->textBrowser->append(QString::fromStdString(line));
}
}
void logdialog::fillLogInfoChronicallySlot()
{
fillloginfo();
}
So, I am able to read the file only on the first call, rest of the calls to get an update from the file are not working.
Thanks in advance
You need to call
std::ios::clear()
in the input stream after the initial reading. When you read the whole file, it sets thefailbit
in the stream and refuses to continue reading, even though the file has changed in the meantime.In your case you must do before reading again:
The complete code is in the following link