I was trying the QFileSystemWatcher
out and it somehow doesn't work as expected. Or am I doing something wrong?
I've set the QFileSystemWatcher
to watch a single file. When I modify the file for the first time, fileChanged()
gets emited, that's OK. But when I modify the file again, fileChanged()
doesn't get emited anymore.
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 answers.
Edit 1
I ran this code under Linux.
Edit 2
I actually need to check all MetaPost files in a tree given by some directory, whether they were modified. I will probably stick to my alternative solution, which is to run QTimer every second and manually check all files. The QFileSystemWatcher probably does this in similar fashion internally, but probably more effectively.