1
QFileSystemWatcher watcher;
watcher.addPath("C:/watch");

QStringList directoryList = watcher.directories();
Q_FOREACH(QString directory, directoryList)
    qDebug() << "Directory name" << directory <<"\n";

DirectoryWatcher* dw = new DirectoryWatcher;

QObject::connect(
    &watcher, SIGNAL(directoryChanged(const QString&)),
    dw,       SLOT(modified(const QString&))
);

QObject::connect(
    &watcher, SIGNAL(fileChanged(QString)),
    dw,       SLOT(modified(QString))
);

In this sample, modified() method called when;

  • a new file created
  • a file deleted
  • a file renamed

But, If i open a file in this folder and modify the content, after I save it, nothing called.

IF I add that specific file to the path like addPath("c:/watch/me.txt") then after modify it gets invoked.

But as you might know, there is a limitation on watcher. So I cannot watch hundreds of files every time.

How can I invoke modified() method on file modifications?

xangr
  • 879
  • 14
  • 28

1 Answers1

2

If you want a cross-platform solution, using Qt5::QFileSystemWatcher, you have no other way than adding each files from the directory you're watching to the QFileSystemWatcher object, hoping that you don't hit the file descriptors limitation.

If you want to use OS specific methods to watch filesystem, you can get some hints from this S/O answer : https://stackoverflow.com/a/931165/228634 but I'm pretty sure you'll have the same limitations.

Community
  • 1
  • 1
Tryum
  • 660
  • 7
  • 22
  • Thanks for your explanation. May I ask, Should I do OS-specific solution which I will watch around 1.000-5.000 files or should I do something like `checkFileDates()` method to check if files are changed like every x minutes? Which one is proper? Or at least os-specific for windows and cross platform for others. – xangr Sep 20 '16 at 09:42
  • I would first implement a cross-platform solution, "checkFileDates()" as you suggest. And if processing is too slow, implement os-specific solutions. – Tryum Sep 20 '16 at 12:30
  • thanks fore the reply! Will do it as you suggest. your answer already accepted. – xangr Sep 20 '16 at 13:23