Escot
17th June 2017, 10:13
I wanted to remove all files from a QFileSystemWatcher so I wrote this:
if (fsWatcher.files().isEmpty() == false)
fsWatcher.removePaths(fsWatcher.files());
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:
if (fsWatcher.files().isEmpty() == false)
{
QStringList fileList = fsWatcher.files();
QStringList::const_iterator filePath;
for (filePath = fileList.constBegin() ; filePath != fileList.constEnd() ; ++filePath)
if (fsWatcher.removePath(*filePath) == false)
qDebug() << "Failed to remove file: " << *filePath;
}
This too didn't work, and it failed for every file.
To investigate the matter further I wrote this program:
#include <QCoreApplication>
#include <QFileSystemWatcher>
#include <QDir>
#include <QDebug>
void AddFiles(QDir & dirReader, QFileSystemWatcher & fsWatcher);
void RemoveFilesQDir(QDir & dirReader, QFileSystemWatcher & fsWatcher);
void RemoveFilesQFSW(QFileSystemWatcher & fsWatcher);
int main()
{
QDir dirReader("G:/TestFiles/Test");
QFileSystemWatcher fsWatcher;
qDebug() << "Remove using RemoveFilesQDir()";
AddFiles(dirReader, fsWatcher);
RemoveFilesQDir(dirReader, fsWatcher);
qDebug() << "\nRemove using RemoveFilesQFSW()";
AddFiles(dirReader, fsWatcher);
RemoveFilesQFSW(fsWatcher);
return 0;
}
void AddFiles(QDir & dirReader, QFileSystemWatcher & fsWatcher)
{
QFileInfoList fileList = dirReader.entryInfoList();
QFileInfoList::const_iterator file;
for (file = fileList.constBegin() ; file != fileList.constEnd() ; ++file)
{
if (file->isFile())
{
if (fsWatcher.addPath(file->filePath()))
qDebug() << "Add Success\t" << file->filePath();
else
qDebug() << "Add Fail\t" << file->filePath();
}
}
}
void RemoveFilesQDir(QDir & dirReader, QFileSystemWatcher & fsWatcher)
{
QFileInfoList fileList = dirReader.entryInfoList();
QFileInfoList::const_iterator file;
for (file = fileList.constBegin() ; file != fileList.constEnd() ; ++file)
{
if (file->isFile())
{
if (fsWatcher.removePath(file->filePath()))
qDebug() << "Remove Success\t" << file->filePath();
else
qDebug() << "Remove Fail\t" << file->filePath();
}
}
}
void RemoveFilesQFSW(QFileSystemWatcher & fsWatcher)
{
if (fsWatcher.files().isEmpty() == false)
{
QStringList fileList = fsWatcher.files();
QStringList::const_iterator file;
for (file = fileList.constBegin() ; file != fileList.constEnd() ; ++file)
{
if (fsWatcher.removePath(*file) == false)
qDebug() << "Remove Successful\t" << *file;
else
qDebug() << "Remove Fail\t" << *file;
}
}
}
The output from the program is this:
Remove using RemoveFilesQDir()
Add Success "G:/TestFiles/Test/Test 01.txt"
Add Success "G:/TestFiles/Test/Test 02.txt"
Add Success "G:/TestFiles/Test/Test 03.txt"
Remove Success "G:/TestFiles/Test/Test 01.txt"
Remove Success "G:/TestFiles/Test/Test 02.txt"
Remove Success "G:/TestFiles/Test/Test 03.txt"
Remove using RemoveFilesQFSW()
Add Success "G:/TestFiles/Test/Test 01.txt"
Add Success "G:/TestFiles/Test/Test 02.txt"
Add Success "G:/TestFiles/Test/Test 03.txt"
Remove Fail "G:/TestFiles/Test/Test 01.txt"
Remove Fail "G:/TestFiles/Test/Test 02.txt"
Remove Fail "G:/TestFiles/Test/Test 03.txt"
Press <RETURN> to close this window...
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?
if (fsWatcher.files().isEmpty() == false)
fsWatcher.removePaths(fsWatcher.files());
Thanks a lot.
if (fsWatcher.files().isEmpty() == false)
fsWatcher.removePaths(fsWatcher.files());
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:
if (fsWatcher.files().isEmpty() == false)
{
QStringList fileList = fsWatcher.files();
QStringList::const_iterator filePath;
for (filePath = fileList.constBegin() ; filePath != fileList.constEnd() ; ++filePath)
if (fsWatcher.removePath(*filePath) == false)
qDebug() << "Failed to remove file: " << *filePath;
}
This too didn't work, and it failed for every file.
To investigate the matter further I wrote this program:
#include <QCoreApplication>
#include <QFileSystemWatcher>
#include <QDir>
#include <QDebug>
void AddFiles(QDir & dirReader, QFileSystemWatcher & fsWatcher);
void RemoveFilesQDir(QDir & dirReader, QFileSystemWatcher & fsWatcher);
void RemoveFilesQFSW(QFileSystemWatcher & fsWatcher);
int main()
{
QDir dirReader("G:/TestFiles/Test");
QFileSystemWatcher fsWatcher;
qDebug() << "Remove using RemoveFilesQDir()";
AddFiles(dirReader, fsWatcher);
RemoveFilesQDir(dirReader, fsWatcher);
qDebug() << "\nRemove using RemoveFilesQFSW()";
AddFiles(dirReader, fsWatcher);
RemoveFilesQFSW(fsWatcher);
return 0;
}
void AddFiles(QDir & dirReader, QFileSystemWatcher & fsWatcher)
{
QFileInfoList fileList = dirReader.entryInfoList();
QFileInfoList::const_iterator file;
for (file = fileList.constBegin() ; file != fileList.constEnd() ; ++file)
{
if (file->isFile())
{
if (fsWatcher.addPath(file->filePath()))
qDebug() << "Add Success\t" << file->filePath();
else
qDebug() << "Add Fail\t" << file->filePath();
}
}
}
void RemoveFilesQDir(QDir & dirReader, QFileSystemWatcher & fsWatcher)
{
QFileInfoList fileList = dirReader.entryInfoList();
QFileInfoList::const_iterator file;
for (file = fileList.constBegin() ; file != fileList.constEnd() ; ++file)
{
if (file->isFile())
{
if (fsWatcher.removePath(file->filePath()))
qDebug() << "Remove Success\t" << file->filePath();
else
qDebug() << "Remove Fail\t" << file->filePath();
}
}
}
void RemoveFilesQFSW(QFileSystemWatcher & fsWatcher)
{
if (fsWatcher.files().isEmpty() == false)
{
QStringList fileList = fsWatcher.files();
QStringList::const_iterator file;
for (file = fileList.constBegin() ; file != fileList.constEnd() ; ++file)
{
if (fsWatcher.removePath(*file) == false)
qDebug() << "Remove Successful\t" << *file;
else
qDebug() << "Remove Fail\t" << *file;
}
}
}
The output from the program is this:
Remove using RemoveFilesQDir()
Add Success "G:/TestFiles/Test/Test 01.txt"
Add Success "G:/TestFiles/Test/Test 02.txt"
Add Success "G:/TestFiles/Test/Test 03.txt"
Remove Success "G:/TestFiles/Test/Test 01.txt"
Remove Success "G:/TestFiles/Test/Test 02.txt"
Remove Success "G:/TestFiles/Test/Test 03.txt"
Remove using RemoveFilesQFSW()
Add Success "G:/TestFiles/Test/Test 01.txt"
Add Success "G:/TestFiles/Test/Test 02.txt"
Add Success "G:/TestFiles/Test/Test 03.txt"
Remove Fail "G:/TestFiles/Test/Test 01.txt"
Remove Fail "G:/TestFiles/Test/Test 02.txt"
Remove Fail "G:/TestFiles/Test/Test 03.txt"
Press <RETURN> to close this window...
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?
if (fsWatcher.files().isEmpty() == false)
fsWatcher.removePaths(fsWatcher.files());
Thanks a lot.