c++ - Qt QFileSystemWatcher: signal fileChanged() gets emited only once -
i trying qfilesystemwatcher
out , somehow doesn't work expected. or doing wrong?
i've set qfilesystemwatcher
watch single file. when modify file first time, filechanged()
gets emited, that's ok. when modify file again, filechanged()
doesn't emited anymore.
here source code:
main.cpp
#include <qapplication> #include "mainwindow.h" int main(int argc, char **argv) { qapplication app(argc, argv); mainwindow window; window.show(); return app.exec(); }
mainwindow.h
#include <qdebug> #include <qfilesystemwatcher> #include <qmainwindow> #include <qstring> class mainwindow : public qmainwindow { q_object public: mainwindow(); private slots: void directorychanged(const qstring & path); void filechanged(const qstring & path); private: qfilesystemwatcher * watcher; };
mainwindow.cpp
#include "mainwindow.h" mainwindow::mainwindow() { watcher = new qfilesystemwatcher(this); connect(watcher, signal(filechanged(const qstring &)), this, slot(filechanged(const qstring &))); connect(watcher, signal(directorychanged(const qstring &)), this, slot(directorychanged(const qstring &))); watcher->addpath("path directory"); watcher->addpath("path file"); } void mainwindow::directorychanged(const qstring & path) { qdebug() << path; } void mainwindow::filechanged(const qstring & path) { qdebug() << path; }
thank answers.
edit 1
i ran code under linux.
edit 2
i need check metapost files in tree given directory, whether modified. stick alternative solution, run qtimer every second , manually check files. qfilesystemwatcher in similar fashion internally, more effectively.
had same problem now. seems qfilesystemwatcher thinks file deleted if it's modified. @ least on linux file system. simple solution was:
watcher->addpath(path);
add above handler of filechanged()
. change word watcher
necessary.
[edit] removed qfile::exists() , handled addpath().