Qt QFileSystemWatcher: signal fileChanged() gets emited only once

I can confirm your problem with current Qt5 and Linux. In addition to the answer given by Peter I solved this problem by adding the following code to the end of the slot-function:

QFileInfo checkFile(path);
while(!checkFile.exists())
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
watcher->addPath(path);

Note that if you add the path immediately, the file often does not exist yet, you get a warning and nothing will be added at all and the watcher looses this path. Therefore, you have to wait/sleep until the file is back to life again, then add it.

Also note that in this example I used C++11 and included and for realizing the sleep.


Had the same problem just now. Seems like QFileSystemWatcher thinks that the file is deleted even if it's only modified. Well at least on Linux file system. My simple solution was:

if (QFile::exists(path)) {
    watcher->addPath(path);
}

Add the above to your handler of fileChanged(). Change the word watcher as necessary.


I had the same problem using Qt5 on Linux. Found out the reason :

Some text editors, like kate, don't modify the contents of a file, but replace the original file with a new file. Replacing a file will delete the old one (IN_DELETE_SELF event), so qt will stop watching the file.

A solution is to also watch the file's directory for creation events.

Tags:

C++

Qt