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.
c ++ qt
pizet
source share