Qt QFileSystemWatcher on Windows

大兔子大兔子 提交于 2019-12-14 03:47:42

问题


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 ;
}

回答1:


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.




回答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



来源:https://stackoverflow.com/questions/11669179/qt-qfilesystemwatcher-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!