PDA

View Full Version : QFileSystemWatcher: doesn't recognize file content modifications in successive instan



Sai Kamat
15th March 2014, 06:53
I display a loading GIF when usbResponse.txt file exists. I hide this GIF when usbResponse.txt is modified. I use two separate threads. mThread to check for the existence of the .txt file and main thread object 'filewatcher' for modifications in the file. The program works perfectly only in the first instance. In successive instances, the 'filewatcher' does not recognize modifications in the usbResponse.txt file. How should I solve this? Please advise.

dialog.h


#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QMovie>
#include <QLabel>

#define GIF_PATH "E:\\QT1\\timeStampPopUp\\timeStampPopUp\\loading.g if"
namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
void displayLoadingGif();

private:
Ui::Dialog *ui;
};

#endif // DIALOG_H

mythread.h


#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QtCore>
#include <QDebug>

#define FILE_PATH "E:\\QT1\\dialogClose2\\dialogClose2\\usbResponse.t xt"

class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = 0);
void run();
QString name;
int exec();
void checkFile();

signals:
void testSignal(QString message);
void fileFoundDisplayGif();

public slots:

};

#endif // MYTHREAD_H

mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFile>
#include <QDebug>
#include <QFileSystemWatcher>
#include "dialog.h"
#include "mythread.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

public slots:
void afterFileHasBeenFound();
void closeModified(const QString &str);

private slots:

private:
Ui::MainWindow *ui;
Dialog *pDialog;
MyThread *mThread;
};

#endif // MAINWINDOW_H

dialog.cpp


#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
displayLoadingGif();
}

Dialog::~Dialog()
{
delete ui;
}

void Dialog::displayLoadingGif()
{
QMovie *pMovie = new QMovie(GIF_PATH);
ui->loadingGifLabel->setMovie(pMovie);
pMovie->start();
}

mythread.cpp


#include "mythread.h"

MyThread::MyThread(QObject *parent) :
QThread(parent)
{
}

void MyThread::run()
{
exec();
}

int MyThread::exec()
{
while(1)
{
checkFile();
emit(testSignal("hello world!!"));
sleep(1);
}
}

void MyThread::checkFile()
{
QFile file(FILE_PATH);
if(file.exists())
{
qDebug()<<"exists";
emit(fileFoundDisplayGif());
}
else
qDebug()<<"doesn't exist";
}

mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pDialog = NULL;
mThread = new MyThread(this);
mThread->name = "mThread";
connect(mThread, SIGNAL(fileFoundDisplayGif()), this, SLOT(afterFileHasBeenFound()), Qt::QueuedConnection);
mThread->start();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::afterFileHasBeenFound()
{
if(pDialog != NULL)
return;
pDialog = new Dialog();
pDialog->setModal(true);
pDialog->show();
}

void MainWindow::closeModified(const QString &str)
{
Q_UNUSED(str)
if(pDialog != NULL)
{
pDialog->hide();
}
QFile file(FILE_PATH);
file.remove();
pDialog = NULL;
}

main.cpp


#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFileSystemWatcher fileWatcher;
fileWatcher.addPath(FILE_PATH);
QStringList fileList = fileWatcher.files();
Q_FOREACH(QString file, fileList)
qDebug() << "File name " << file;
MainWindow* mc = new MainWindow;
QObject::connect(&fileWatcher, SIGNAL(fileChanged(QString)), mc, SLOT(closeModified(QString)));
mc->show();

return a.exec();
}

Scorp2us
19th March 2014, 16:55
This may be a platforms specific limitation. that class just wraps whatever the OS provides. I'd look into window's implementation.