PDA

View Full Version : QFileSystemWatcher problem... yes, again..



nateriver
31st December 2009, 16:47
Here's the code:

FileWatcherTest::FileWatcherTest() {
w = new QFileSystemWatcher;
w->addPath("C:\\tmp");
this->reindex("C:\\tmp");

QObject::connect(w,SIGNAL(directoryChanged(QString )),this,SLOT(updateDir(QString)));
QObject::connect(w,SIGNAL(fileChanged(QString)),th is,SLOT(updateFile(QString)));
}

void FileWatcherTest::reindex(const QString &path) {
w->removePaths(w->files() + w->directories());
w->addPath("C:\\tmp");
this->doReindex(path);
}

void FileWatcherTest::doReindex(const QString &path) {
QDir dir(path);
if (!dir.exists()) {
return;
} else {
foreach(QFileInfo inf, dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot, QDir::DirsLast)) {
if (inf.isDir()) {
this->doReindex(inf.absoluteFilePath());
if (!w->directories().contains(inf.absoluteFilePath())) {
qDebug() << "adding dir" << inf.absoluteFilePath();
w->addPath(inf.absoluteFilePath());
}
} else {
if (!w->files().contains(inf.absoluteFilePath())) {
qDebug() << "adding file" << inf.absoluteFilePath();
w->addPath(inf.absoluteFilePath());
}
}
}
}
}

void FileWatcherTest::updateDir(const QString &path) {
qDebug() << "updated dir:" << path;
this->reindex("C:\\tmp");

qDebug() << "Watched paths:";
foreach(QString p, w->files() + w->directories()) {
qDebug() << "\t" << p;
}
}

void FileWatcherTest::updateFile(const QString &path) {
qDebug() << "updated file:" << path;

qDebug() << "Watched paths:";
foreach(QString p, w->files() + w->directories()) {
qDebug() << "\t" << p;
}
}
This hacky code watches directory "C:\tmp" recursively (if something is added to it, it is also watched) and outputs what's modified and what's currently watched to console each time something is modified (whether it is a directory or a file).
Everything works quite well (except for some "unknown message ' " (note the single quote in the end - it's not a typo) messages output sometimes when dealing with non-latin symbols in filenames and qfilesystemwatcher errors when deleting non-empty directories (since it emits fileChanged for each file deleted in directory when the directory itself is already deleted) - but those can be safely ignored). Now let's say we have the following directory structure:


C:
---tmp\
---------file1.txt
---------file2.txt
---------tmp2\
----------------file3.txt
----------------file4.txt

If we modify any of the files, or add any content to the directories, everything works as expected. But if we modify any file, and then delete any other file, all files left are still watched by QFileSystemWatcher (at least it reports that with QFileSystemWatcher::files() and output to console in my code), but signals are not emitted upon their modification! So if we modify file after we deleted another file, it's modification will not be "seen" by QFileSystemWatcher.
Qt 4.6, Windows XP SP3, NTFS used for working hard drive filesystem.
Any input on this issue will be greatly appreciated.

P.S. Happy New Year! ;)