5

I have the following issue: I create a QFileSystemWatcher and it runs and works nicely on Linux, but no way on Windows 7. Can you spot anything in the code that might make it not to work?

Thx.

Here is the code to initialize it:

mConfigChangeWatcher = new QFileSystemWatcher();
mConfigChangeWatcher->addPath(config_file_name);

QObject::connect(mConfigChangeWatcher,
                 SIGNAL(fileChanged(QString)),
                 this,
                 SLOT(configFileChanged(QString)));

and this is supposed to be the slot getting the work done:

void MyClass::configFileChanged(const QString &file)
{
    qDebug() << "Changed: " << file ;
}
NG_
  • 6,895
  • 7
  • 45
  • 67
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • Maybe the path has to be represented differently? – Herr von Wurst Jul 26 '12 at 12:43
  • the path is like: `("C:/Users/fela/work/d_RTI-1638/build/Applications/Debug/conf/configuration.xml")` (this is the output from the mConfigChangeWatcher->files() – Ferenc Deak Jul 26 '12 at 12:45
  • I wrote a simple application, and it works. Maybe for our case it has to do with the fact that we move the app to a different thread, and then somehow signals gets lost ... – Ferenc Deak Aug 06 '12 at 07:33

2 Answers2

6

When you check if the file is added to the watcher using QFileSystemWatcher::files() method after the first modification in the file do you get the correct list?

I was with the issue that some applications, when modifing a file, delete the old file from the system and write it again.

Note that QFileSystemWatcher stops monitoring files once they have been renamed or removed from disk, and directories once they have been removed from disk.

I was using QFileSystemWatcher to watch an image file edited by Photoshop. Somehow the file gets removed from the list of files being watched.

McLeary
  • 1,231
  • 2
  • 13
  • 21
  • thank you! I didn't even assume file can be deleted and then created when you save it is some application. This explains why my QFileSystemWatcher stops monitoring a file when I edit it – Dmitriy Feb 06 '14 at 13:48
  • BTW, I still don't know a proper way to handle this problem. My workaround was to use a single shot timer and then add the file again to be monitored. This doesn't work for all cases but solves my problem at the time. – McLeary Feb 06 '14 at 23:50
  • :) I used the exactly same workaround. The other workaround I was thinking about is to monitor the directory that holds the file. But the first one is simpler and works well for me – Dmitriy Feb 07 '14 at 11:23
2

I had the same problem and solved it very fast. Within the slot that manages the fileChanged signal I noted the path disappears from files(). I simply make a check and re-add it if necessary

if (! watcher_.files().contains(path))
{
    watcher_.addPath(path);
}

I hope this helps

Fabio

manuell
  • 7,528
  • 5
  • 31
  • 58