File changed in QFileSystemWatcher is never triggered
-
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 notfileChanged
-
@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.
-
addPath() returns a bool to see ifit was successful. Also is backup a directory or a file?
See the documentation. -
@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.
-
@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.
-
@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? -
@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.
-