I wanted to remove all files from a QFileSystemWatcher so I wrote this:

Qt Code:
  1. if (fsWatcher.files().isEmpty() == false)
  2. fsWatcher.removePaths(fsWatcher.files());
To copy to clipboard, switch view to plain text mode 
However, I found it didn't work, and checking the QStringList returned by removePaths() I found it was failing for every single file, so no files were ever being removed from the watcher list.

Since removePaths() wasn't working I thought I'd try the removePath() method with the code:

Qt Code:
  1. if (fsWatcher.files().isEmpty() == false)
  2. {
  3. QStringList fileList = fsWatcher.files();
  4. QStringList::const_iterator filePath;
  5. for (filePath = fileList.constBegin() ; filePath != fileList.constEnd() ; ++filePath)
  6. if (fsWatcher.removePath(*filePath) == false)
  7. qDebug() << "Failed to remove file: " << *filePath;
  8. }
To copy to clipboard, switch view to plain text mode 
This too didn't work, and it failed for every file.

To investigate the matter further I wrote this program:

Qt Code:
  1. #include <QCoreApplication>
  2. #include <QFileSystemWatcher>
  3. #include <QDir>
  4. #include <QDebug>
  5.  
  6. void AddFiles(QDir & dirReader, QFileSystemWatcher & fsWatcher);
  7. void RemoveFilesQDir(QDir & dirReader, QFileSystemWatcher & fsWatcher);
  8. void RemoveFilesQFSW(QFileSystemWatcher & fsWatcher);
  9.  
  10.  
  11. int main()
  12. {
  13. QDir dirReader("G:/TestFiles/Test");
  14. QFileSystemWatcher fsWatcher;
  15.  
  16. qDebug() << "Remove using RemoveFilesQDir()";
  17. AddFiles(dirReader, fsWatcher);
  18. RemoveFilesQDir(dirReader, fsWatcher);
  19.  
  20. qDebug() << "\nRemove using RemoveFilesQFSW()";
  21. AddFiles(dirReader, fsWatcher);
  22. RemoveFilesQFSW(fsWatcher);
  23.  
  24. return 0;
  25. }
  26.  
  27.  
  28. void AddFiles(QDir & dirReader, QFileSystemWatcher & fsWatcher)
  29. {
  30. QFileInfoList fileList = dirReader.entryInfoList();
  31. QFileInfoList::const_iterator file;
  32. for (file = fileList.constBegin() ; file != fileList.constEnd() ; ++file)
  33. {
  34. if (file->isFile())
  35. {
  36. if (fsWatcher.addPath(file->filePath()))
  37. qDebug() << "Add Success\t" << file->filePath();
  38. else
  39. qDebug() << "Add Fail\t" << file->filePath();
  40. }
  41. }
  42. }
  43.  
  44. void RemoveFilesQDir(QDir & dirReader, QFileSystemWatcher & fsWatcher)
  45. {
  46. QFileInfoList fileList = dirReader.entryInfoList();
  47. QFileInfoList::const_iterator file;
  48. for (file = fileList.constBegin() ; file != fileList.constEnd() ; ++file)
  49. {
  50. if (file->isFile())
  51. {
  52. if (fsWatcher.removePath(file->filePath()))
  53. qDebug() << "Remove Success\t" << file->filePath();
  54. else
  55. qDebug() << "Remove Fail\t" << file->filePath();
  56. }
  57. }
  58. }
  59.  
  60.  
  61. void RemoveFilesQFSW(QFileSystemWatcher & fsWatcher)
  62. {
  63. if (fsWatcher.files().isEmpty() == false)
  64. {
  65. QStringList fileList = fsWatcher.files();
  66. QStringList::const_iterator file;
  67. for (file = fileList.constBegin() ; file != fileList.constEnd() ; ++file)
  68. {
  69. if (fsWatcher.removePath(*file) == false)
  70. qDebug() << "Remove Successful\t" << *file;
  71. else
  72. qDebug() << "Remove Fail\t" << *file;
  73. }
  74. }
  75. }
To copy to clipboard, switch view to plain text mode 
The output from the program is this:

Qt Code:
  1. Remove using RemoveFilesQDir()
  2. Add Success "G:/TestFiles/Test/Test 01.txt"
  3. Add Success "G:/TestFiles/Test/Test 02.txt"
  4. Add Success "G:/TestFiles/Test/Test 03.txt"
  5. Remove Success "G:/TestFiles/Test/Test 01.txt"
  6. Remove Success "G:/TestFiles/Test/Test 02.txt"
  7. Remove Success "G:/TestFiles/Test/Test 03.txt"
  8.  
  9. Remove using RemoveFilesQFSW()
  10. Add Success "G:/TestFiles/Test/Test 01.txt"
  11. Add Success "G:/TestFiles/Test/Test 02.txt"
  12. Add Success "G:/TestFiles/Test/Test 03.txt"
  13. Remove Fail "G:/TestFiles/Test/Test 01.txt"
  14. Remove Fail "G:/TestFiles/Test/Test 02.txt"
  15. Remove Fail "G:/TestFiles/Test/Test 03.txt"
  16. Press <RETURN> to close this window...
To copy to clipboard, switch view to plain text mode 
So, removing the files using the file paths from the QDir::entryInfoList() works fine, but removing the files using the paths returned by QFileSystemWatcher::files() fails, even though the file paths are identical in both cases.

I was just wondering if somebody could explain why removing files using the paths returned by QFileSystemWatcher::files() fails?

Also, since the below code doesn't work, what's the fastest way to clear all the files from a QFileSystemWatcher?

Qt Code:
  1. if (fsWatcher.files().isEmpty() == false)
  2. fsWatcher.removePaths(fsWatcher.files());
To copy to clipboard, switch view to plain text mode 
Thanks a lot.