Qt QFileSystemWatcher: fileChanged () signal gets only once

I tried QFileSystemWatcher and it somehow does not work properly. Or am I doing something wrong?

I installed QFileSystemWatcher to view a single file. When I modify a file for the first time, fileChanged() receives radiation, this is normal. But when I modify the file again, fileChanged() will no longer be released.

Here is the 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 to directory"); watcher->addPath("path to file"); } void MainWindow::directoryChanged(const QString & path) { qDebug() << path; } void MainWindow::fileChanged(const QString & path) { qDebug() << path; } 

Thank you for your responses.

Change 1

I ran this code under Linux.

Edit 2

I really need to check all the MetaPost files in the tree specified by some directory, whether they have been changed. I probably stick with my alternative solution, which is to run QTimer every second and manually check all the files. QFileSystemWatcher probably does this in a similar way internally, but is probably more efficient.

+7
c ++ qt
source share
3 answers

There was the same problem just now. It seems that QFileSystemWatcher believes that the file is deleted, even if it is only modified. Well, at least on the Linux file system. My simple solution was:

 watcher->addPath(path); 

Add the above to the fileChanged() handler. If necessary, change the word watcher .

[Edit] Deleted QFile :: exists (), it is processed using addPath ().

+8
source share

I had the same issue with Qt5 on Linux. Found out the reason:

Some text editors, such as kate, do not change the contents of the file, but replace the original file with a new file. Replacing the file will delete the old ( IN_DELETE_SELF event), so qt will stop watching the file.

The solution is also to browse the file directory for creating events.

+7
source share

I can confirm your problem with current Qt5 and Linux. In addition to what Peter gave, 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); 

Please note: if you add the path immediately, the file often does not exist yet, you will receive a warning and nothing will be added at all, and the observer will lose this path. So you have to wait / sleep until the file comes back to life, and then add it.

Also note that in this example I used C ++ 11 and turned it on to implement sleep.

+2
source share

All Articles