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. File changed in QFileSystemWatcher is never triggered

File changed in QFileSystemWatcher is never triggered

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.2k Views
  • 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.
  • A Offline
    A Offline
    AkshayFP
    wrote on last edited by AkshayFP
    #1

    I am trying to get QFileSystemWatcher::fileChanged trigger by creating a text file in the added path.

    I am using Qt 6.5 on MSVC2019.

    Here is an example code:

    main.cpp

    FileWatchService watcher(&app);
    watcher.addPath("C:\\Users\\gollaha\\backup");
    

    filewatchservice.h

    class FileWatchService : public QObject
    {
        Q_OBJECT
    public:
        explicit FileWatchService(QObject *parent = nullptr);
        ~FileWatchService();
    
        QFileSystemWatcher *_watcher;
    
        void addPath(const QString &path);
    
    signals:
        void contentChanged(const QString &path, const QString whatChanged);
    
    private slots:
        void directoryChanged(const QString &path);
        void fileChanged(const QString &path);
    };
    

    filewatchservice.cpp

    FileWatchService::FileWatchService(QObject *parent)
        : QObject{parent}
    {
        _watcher = new QFileSystemWatcher(this);
    
        connect(_watcher, &QFileSystemWatcher::fileChanged, this, &FileWatchService::fileChanged);
        connect(_watcher, &QFileSystemWatcher::directoryChanged, this, &FileWatchService::directoryChanged);
    }
    
    FileWatchService::~FileWatchService()
    {
        if (_watcher != NULL) {
            delete _watcher;
        }
    }
    
    void FileWatchService::addPath(const QString &path)
    {
        _watcher->addPath(path);
    }
    
    void FileWatchService::directoryChanged(const QString &path)
    {
        qDebug() << "directory" << path;
        emit contentChanged(path, "director");
    }
    
    void FileWatchService::fileChanged(const QString &path)
    {
        qDebug() << "file" << path;
    
        emit contentChanged(path, "file");
    }
    

    fileChanged is never triggered even though I added a text to the path. Any help here is appreciated.


    Update

    I have also tried this:

    main.cpp

    QFileSystemWatcher *_watcher = new QFileSystemWatcher(&app);
    _watcher->addPath("C:\\Users\\gollaha\\backup");
    QObject::connect(_watcher, &QFileSystemWatcher::fileChanged, &app, [](QString file){qDebug() << file;});
    QObject::connect(_watcher, &QFileSystemWatcher::directoryChanged, &app, [](QString file){qDebug() << file;});
    

    I am able to trigger directoryChanged but not fileChanged

    Christian EhrlicherC 1 Reply Last reply
    0
    • A AkshayFP

      @Christian-Ehrlicher Wait, so when does fileChanged triggered? That means every time I add a file or a folder, I need to add the path of it to the watcher?

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by ChrisW67
      #6

      @AkshayFP As the docs say, fileChanged() is triggered if path is not a directory. You cannot watch a file that does not exist. If you create or modify a file in a directory that is monitored then directoryChanged() is emitted.

      On Linux this looks like:

      /tmp/test$ cat main.cpp
      #include <QCoreApplication>
      #include <QDebug>
      #include <QFileSystemWatcher>
      
      int main(int argc, char **argv) {
              QCoreApplication app(argc, argv);
      
              QFileSystemWatcher w;
              w.addPath("/tmp/test");
              QObject::connect(&w, &QFileSystemWatcher::fileChanged,      &app, [](QString file){qDebug() << "file:" << file;});
              QObject::connect(&w, &QFileSystemWatcher::directoryChanged, &app, [](QString file){qDebug() << "dir: " << file;});
      
              return app.exec();
      }
      
      /tmp/test$ make
      make: Nothing to be done for 'first'.
      
      /tmp/test$ ./test &
      [1] 4227
      
      /tmp/test$ touch . # modify the directory directly
      dir:  "/tmp/test"
      
      /tmp/test$ touch anewfile # new file in the directory
      dir:  "/tmp/test"
      
      /tmp/test$ touch main.cpp # an existing file is modified
      dir:  "/tmp/test"
      
      /tmp/test$ rm anewfile # delete the file we created
      dir:  "/tmp/test"
      

      I imagine that Windows behaviour is the same.

      1 Reply Last reply
      3
      • A AkshayFP

        I am trying to get QFileSystemWatcher::fileChanged trigger by creating a text file in the added path.

        I am using Qt 6.5 on MSVC2019.

        Here is an example code:

        main.cpp

        FileWatchService watcher(&app);
        watcher.addPath("C:\\Users\\gollaha\\backup");
        

        filewatchservice.h

        class FileWatchService : public QObject
        {
            Q_OBJECT
        public:
            explicit FileWatchService(QObject *parent = nullptr);
            ~FileWatchService();
        
            QFileSystemWatcher *_watcher;
        
            void addPath(const QString &path);
        
        signals:
            void contentChanged(const QString &path, const QString whatChanged);
        
        private slots:
            void directoryChanged(const QString &path);
            void fileChanged(const QString &path);
        };
        

        filewatchservice.cpp

        FileWatchService::FileWatchService(QObject *parent)
            : QObject{parent}
        {
            _watcher = new QFileSystemWatcher(this);
        
            connect(_watcher, &QFileSystemWatcher::fileChanged, this, &FileWatchService::fileChanged);
            connect(_watcher, &QFileSystemWatcher::directoryChanged, this, &FileWatchService::directoryChanged);
        }
        
        FileWatchService::~FileWatchService()
        {
            if (_watcher != NULL) {
                delete _watcher;
            }
        }
        
        void FileWatchService::addPath(const QString &path)
        {
            _watcher->addPath(path);
        }
        
        void FileWatchService::directoryChanged(const QString &path)
        {
            qDebug() << "directory" << path;
            emit contentChanged(path, "director");
        }
        
        void FileWatchService::fileChanged(const QString &path)
        {
            qDebug() << "file" << path;
        
            emit contentChanged(path, "file");
        }
        

        fileChanged is never triggered even though I added a text to the path. Any help here is appreciated.


        Update

        I have also tried this:

        main.cpp

        QFileSystemWatcher *_watcher = new QFileSystemWatcher(&app);
        _watcher->addPath("C:\\Users\\gollaha\\backup");
        QObject::connect(_watcher, &QFileSystemWatcher::fileChanged, &app, [](QString file){qDebug() << file;});
        QObject::connect(_watcher, &QFileSystemWatcher::directoryChanged, &app, [](QString file){qDebug() << file;});
        

        I am able to trigger directoryChanged but not fileChanged

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #2

        addPath() returns a bool to see ifit was successful. Also is backup a directory or a file?
        See the documentation.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        A 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          addPath() returns a bool to see ifit was successful. Also is backup a directory or a file?
          See the documentation.

          A Offline
          A Offline
          AkshayFP
          wrote on last edited by AkshayFP
          #3

          @Christian-Ehrlicher it does return true. And backup is a folder name. When I try to add a file to backup folder, only the directoryChanged is detected.

          Christian EhrlicherC 1 Reply Last reply
          0
          • A AkshayFP

            @Christian-Ehrlicher it does return true. And backup is a folder name. When I try to add a file to backup folder, only the directoryChanged is detected.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @AkshayFP said in File changed in QFileSystemWatcher is never triggered:

            When I try to add a file to backup folder, only the directoryChanged is detected.

            So it's working as expected.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            A 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @AkshayFP said in File changed in QFileSystemWatcher is never triggered:

              When I try to add a file to backup folder, only the directoryChanged is detected.

              So it's working as expected.

              A Offline
              A Offline
              AkshayFP
              wrote on last edited by AkshayFP
              #5

              @Christian-Ehrlicher Wait, so when does fileChanged triggered? That means every time I add a file or a folder, I need to add the path of it to the watcher?

              C 1 Reply Last reply
              0
              • A AkshayFP

                @Christian-Ehrlicher Wait, so when does fileChanged triggered? That means every time I add a file or a folder, I need to add the path of it to the watcher?

                C Offline
                C Offline
                ChrisW67
                wrote on last edited by ChrisW67
                #6

                @AkshayFP As the docs say, fileChanged() is triggered if path is not a directory. You cannot watch a file that does not exist. If you create or modify a file in a directory that is monitored then directoryChanged() is emitted.

                On Linux this looks like:

                /tmp/test$ cat main.cpp
                #include <QCoreApplication>
                #include <QDebug>
                #include <QFileSystemWatcher>
                
                int main(int argc, char **argv) {
                        QCoreApplication app(argc, argv);
                
                        QFileSystemWatcher w;
                        w.addPath("/tmp/test");
                        QObject::connect(&w, &QFileSystemWatcher::fileChanged,      &app, [](QString file){qDebug() << "file:" << file;});
                        QObject::connect(&w, &QFileSystemWatcher::directoryChanged, &app, [](QString file){qDebug() << "dir: " << file;});
                
                        return app.exec();
                }
                
                /tmp/test$ make
                make: Nothing to be done for 'first'.
                
                /tmp/test$ ./test &
                [1] 4227
                
                /tmp/test$ touch . # modify the directory directly
                dir:  "/tmp/test"
                
                /tmp/test$ touch anewfile # new file in the directory
                dir:  "/tmp/test"
                
                /tmp/test$ touch main.cpp # an existing file is modified
                dir:  "/tmp/test"
                
                /tmp/test$ rm anewfile # delete the file we created
                dir:  "/tmp/test"
                

                I imagine that Windows behaviour is the same.

                1 Reply Last reply
                3
                • A AkshayFP has marked this topic as solved on

                • Login

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