You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Wednesday, April 11th 2012, 1:39pm

problem using QFileSystemWatcher,unable to show log file output

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

2

Wednesday, April 11th 2012, 4:10pm

your code is virtually impossible to read due to lack of code tags.

it also does not compile.

e.g.
QFileSystemWatcher fileWatcher = new QFileSystemWatcher(this) ;

please read my sig.

Source code

1
2
3
4
5
void logFile::readContents(QString path) 
 { 
QFile *logFile; <<<----- pointer points to anywhere

if(!logFile->open(QIODevice::ReadOnly))   <<<------ pointer still points to random address.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.