0

I created a simple watcher and added a path to it. I connected the watcher's signal to my method:

void CssSetter::watchIfNeeded(const QString& newPath)
{
    if(watcher==nullptr) {
      watcher = new QFileSystemWatcher(this);
      connect(watcher, &QFileSystemWatcher::fileChanged, this, &CssSetter::cssFileChanged);
    }
    watcher->addPath(newPath);
}

The callback looks like this:

void CssSetter::cssFileChanged(const QString& path)
{

  qDebug()<<"CSS "<<path<<" changed!";
  QFileInfo css_file(path);
  qDebug()<<"[CSS] Watcher: "<<path<<(css_file.isFile()?" exists!":" doesn't exist!");
  // Should update CSS file, but fails because it also has `.exists()` checks inside
  setCSSFromPath(path);
}

The output is:

csssetter.cpp:81 (void CssSetter::cssFileChanged(const QString&)): CSS  "/storage/extSdCard/w98.css"  changed!
csssetter.cpp:84 (void CssSetter::cssFileChanged(const QString&)): [CSS] Watcher:  "/storage/extSdCard/w98.css"  doesn't exist!
csssetter.cpp:24 (void CssSetter::setCSSFromPath(QFile&)): [CSS]  "/storage/extSdCard/w98.css"  doesn't exist!

The last line comes from the function normally responsible from loading the file if it exists. When I tried to override the control, it didn't work. Reading the file when in the file watcher's callback is not possible.

How can I properly read the file when QFileSystemWatcher reports that it has changed? Is this intended, or is it a bug?

peppe
  • 21,934
  • 4
  • 55
  • 70
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • What kind of change did you apply to the file? Do you suspect a spurious warning or was the file actually removed at some point? – peppe Mar 16 '16 at 17:21
  • I copied new version of the file from the computer to Android phone using the "audio device" API or whatever is it called. I have suspition OS deletes the file prior to writing the new copy. If that is so, Qt is not doing anything wrong but I still need to fix it somehow. – Tomáš Zato Mar 16 '16 at 17:36
  • Wait (and retry) after a little while? Try to get notifications not (only) for the file, but for its parent directory as well? – peppe Mar 16 '16 at 17:50
  • The latter suggestion sounds better, I'll try that tomorrow. Thanks a lot for the idea :) – Tomáš Zato Mar 16 '16 at 18:15

0 Answers0