I am using QFileSystemWatcher to display the output of a log file to a QTextEdit.But i am still not getting the result,i think there is some mistake somewhere in my code,i can’t figure out.
Ui form appears for a fraction of second and then the application terminates unexpectedly.
here’s the code:-
LOGFILE.h
public slots:
void readContents(QString);
LOGFILE.cpp
logFile::logFile(QWidget *parent):QMainWindow(parent),ui(new Ui::logFile)
{
QFile *logFile = new QFile(" /var/log/system.log");
new QTextStream(logFile);
QFileSystemWatcher fileWatcher = new QFileSystemWatcher(this) ;
fileWatcher->addPath ("/var/log/system.log");
QObject::connect(fileWatcher, SIGNAL(fileChanged(QString)), this, SLOT(readContents(QString))) ;
}
void logFile::readContents(QString path)
{
QFile *logFile;
QString textAppended ;
int newSize = logFile->size();
int oldSize;
if(newSize > oldSize)
{
if(!logFile->open(QIODevice::ReadOnly))
{
textAppended = "Error in opening the File";
//showOutput();
ui->logTextEdit->setPlainText(textAppended);
return;
}
else
{
logFile->seek((logFile->size() - 1) - (newSize - oldSize));
QTextStream txtStream(logFile);
while(!txtStream.atEnd())
{
textAppended = txtStream.readLine();
}
// showOutput();
ui->logTextEdit->setPlainText(textAppended);
}
oldSize = newSize;
logFile->close();
}
}