Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileSystemWatcher: does not recognize file content modifications in successive instances

QFileSystemWatcher: does not recognize file content modifications in successive instances

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 498 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sai Kamat
    wrote on last edited by
    #1

    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.

    Code:
    dialog.h
    @
    #ifndef DIALOG_H
    #define DIALOG_H

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

    #define GIF_PATH "E:\QT1\timeStampPopUp\timeStampPopUp\loading.gif"
    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.txt"

    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(&#41;;
    

    }
    @

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved