Results 1 to 2 of 2

Thread: QFileSystemWatcher: doesn't recognize file content modifications in successive instan

  1. #1
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QFileSystemWatcher: doesn't recognize file content modifications in successive instan

    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
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QMovie>
    6. #include <QLabel>
    7.  
    8. #define GIF_PATH "E:\\QT1\\timeStampPopUp\\timeStampPopUp\\loading.gif"
    9. namespace Ui {
    10. class Dialog;
    11. }
    12.  
    13. class Dialog : public QDialog
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit Dialog(QWidget *parent = 0);
    19. ~Dialog();
    20. void displayLoadingGif();
    21.  
    22. private:
    23. Ui::Dialog *ui;
    24. };
    25.  
    26. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 
    mythread.h
    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QtCore>
    6. #include <QDebug>
    7.  
    8. #define FILE_PATH "E:\\QT1\\dialogClose2\\dialogClose2\\usbResponse.txt"
    9.  
    10. class MyThread : public QThread
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit MyThread(QObject *parent = 0);
    15. void run();
    16. QString name;
    17. int exec();
    18. void checkFile();
    19.  
    20. signals:
    21. void testSignal(QString message);
    22. void fileFoundDisplayGif();
    23.  
    24. public slots:
    25.  
    26. };
    27.  
    28. #endif // MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QFile>
    6. #include <QDebug>
    7. #include <QFileSystemWatcher>
    8. #include "dialog.h"
    9. #include "mythread.h"
    10.  
    11. namespace Ui {
    12. class MainWindow;
    13. }
    14.  
    15. class MainWindow : public QMainWindow
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. explicit MainWindow(QWidget *parent = 0);
    21. ~MainWindow();
    22.  
    23. public slots:
    24. void afterFileHasBeenFound();
    25. void closeModified(const QString &str);
    26.  
    27. private slots:
    28.  
    29. private:
    30. Ui::MainWindow *ui;
    31. Dialog *pDialog;
    32. MyThread *mThread;
    33. };
    34.  
    35. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. Dialog::Dialog(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::Dialog)
    7. {
    8. ui->setupUi(this);
    9. displayLoadingGif();
    10. }
    11.  
    12. Dialog::~Dialog()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void Dialog::displayLoadingGif()
    18. {
    19. QMovie *pMovie = new QMovie(GIF_PATH);
    20. ui->loadingGifLabel->setMovie(pMovie);
    21. pMovie->start();
    22. }
    To copy to clipboard, switch view to plain text mode 
    mythread.cpp
    Qt Code:
    1. #include "mythread.h"
    2.  
    3. MyThread::MyThread(QObject *parent) :
    4. QThread(parent)
    5. {
    6. }
    7.  
    8. void MyThread::run()
    9. {
    10. exec();
    11. }
    12.  
    13. int MyThread::exec()
    14. {
    15. while(1)
    16. {
    17. checkFile();
    18. emit(testSignal("hello world!!"));
    19. sleep(1);
    20. }
    21. }
    22.  
    23. void MyThread::checkFile()
    24. {
    25. QFile file(FILE_PATH);
    26. if(file.exists())
    27. {
    28. qDebug()<<"exists";
    29. emit(fileFoundDisplayGif());
    30. }
    31. else
    32. qDebug()<<"doesn't exist";
    33. }
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. pDialog = NULL;
    10. mThread = new MyThread(this);
    11. mThread->name = "mThread";
    12. connect(mThread, SIGNAL(fileFoundDisplayGif()), this, SLOT(afterFileHasBeenFound()), Qt::QueuedConnection);
    13. mThread->start();
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. }
    20.  
    21. void MainWindow::afterFileHasBeenFound()
    22. {
    23. if(pDialog != NULL)
    24. return;
    25. pDialog = new Dialog();
    26. pDialog->setModal(true);
    27. pDialog->show();
    28. }
    29.  
    30. void MainWindow::closeModified(const QString &str)
    31. {
    32. Q_UNUSED(str)
    33. if(pDialog != NULL)
    34. {
    35. pDialog->hide();
    36. }
    37. QFile file(FILE_PATH);
    38. file.remove();
    39. pDialog = NULL;
    40. }
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. QFileSystemWatcher fileWatcher;
    8. fileWatcher.addPath(FILE_PATH);
    9. QStringList fileList = fileWatcher.files();
    10. Q_FOREACH(QString file, fileList)
    11. qDebug() << "File name " << file;
    12. MainWindow* mc = new MainWindow;
    13. QObject::connect(&fileWatcher, SIGNAL(fileChanged(QString)), mc, SLOT(closeModified(QString)));
    14. mc->show();
    15.  
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2008
    Posts
    45
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QFileSystemWatcher: doesn't recognize file content modifications in successive in

    This may be a platforms specific limitation. that class just wraps whatever the OS provides. I'd look into window's implementation.

Similar Threads

  1. Replies: 7
    Last Post: 3rd February 2014, 14:27
  2. QTestLib doesn't recognize NULL pointers?
    By entonjackson in forum Qt Programming
    Replies: 5
    Last Post: 12th October 2011, 13:10
  3. Replies: 1
    Last Post: 22nd December 2010, 17:56
  4. Replies: 1
    Last Post: 23rd July 2010, 13:10
  5. Phonon: how to recognize a media file?
    By YaK in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2009, 08:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.