View Full Version : QFileSystemModel::remove not removing index from model

24th February 2011, 10:27

When I remove an index from QFileSystemModel, the file is being deleted and no longer exists but if I call QFileSystemModel::index with the path of the file I have just removed, it is still a valid index, but in the docs, it says that ::remove should remove the index from the model and that ::isValid on an index only returns true if the index has a model.

I have tried to create a minimal example below:

QString dirPath = "C:\\TestQFSM";
QString file1Path = "C:\\TestQFSM\\file1";
QString file2Path = "C:\\TestQFSM\\file2";
QString file3Path = "C:\\TestQFSM\\file3";

QDir cDrive = QDir("C:\\");
bool madeDirectory = cDrive.mkdir(dirPath);
qDebug() << "Made directory? " << madeDirectory << " expecting true";

QFile file1(file1Path);
file1.open( QIODevice::WriteOnly );
QTextStream file1Stream(&file1);
file1Stream << "Some text";
bool file1Exists = QFile(file1Path).exists();
qDebug() << "File1Exists? " << file1Exists << " expecting true";

QFile file2(file2Path);
file2.open( QIODevice::WriteOnly );
QTextStream file2Stream(&file2);
file2Stream << "Some text";
bool file2Exists = QFile(file2Path).exists();
qDebug() << "File2Exists? " << file2Exists << " expecting true";

bool file3Exists = QFile(file3Path).exists();
qDebug() << "File3Exists? " << file3Exists << " expecting false";

QFileSystemModel* fileSystemModel = new QFileSystemModel(this);

bool file1Valid = fileSystemModel->index(file1Path).isValid();
qDebug() << "File1 Valid? " << file1Valid << " expecting true";

bool file2Valid = fileSystemModel->index(file2Path).isValid();
qDebug() << "File2 Valid? " << file2Valid << " expecting true";

bool file3Valid = fileSystemModel->index(file3Path).isValid();
qDebug() << "File3 Valid? " << file3Valid << " expecting false";

bool file1Removed = fileSystemModel->remove(fileSystemModel->index(file1Path));
qDebug() << "File1 Removed? " << file1Removed << " expecting true";

bool file2Removed = fileSystemModel->remove(fileSystemModel->index(file2Path));
qDebug() << "File2 Removed? " << file2Removed << " expecting true";

bool file3Removed = fileSystemModel->remove(fileSystemModel->index(file3Path));
qDebug() << "File3 Removed? " << file3Removed << " expecting false";

file1Exists = QFile(file1Path).exists();
qDebug() << "File1Exists after removal? " << file1Exists << " expecting false";

file2Exists = QFile(file2Path).exists();
qDebug() << "File2Exists after removal? " << file2Exists << " expecting false";

file3Exists = QFile(file3Path).exists();
qDebug() << "File3Exists after removal? " << file3Exists << " expecting false";

file1Valid = fileSystemModel->index(file1Path).isValid();
qDebug() << "File1 Valid after removal? " << file1Valid << " expecting false";

file2Valid = fileSystemModel->index(file2Path).isValid();
qDebug() << "File2 Valid after removal? " << file2Valid << " expecting false";

file3Valid = fileSystemModel->index(file3Path).isValid();
qDebug() << "File3 Valid after removal? " << file3Valid << " expecting false";

The output I am getting is:

Made directory? true expecting true
File1Exists? true expecting true
File2Exists? true expecting true
File3Exists? false expecting false
File1 Valid? true expecting true
File2 Valid? true expecting true
File3 Valid? false expecting false
File1 Removed? true expecting true
File2 Removed? true expecting true
QFileSystemWatcher::removePath: path is empty
QFile::remove: Empty or null file name
File3 Removed? false expecting false
File1Exists after removal? false expecting false
File2Exists after removal? false expecting false
File3Exists after removal? false expecting false
File1 Valid after removal? true expecting false
File2 Valid after removal? true expecting false
File3 Valid after removal? false expecting false

So all the values are as expected apart from the ones marked with red. Can someone explain why the indices are still valid?

Thanks for your time,

Matt Grover

27th February 2011, 09:24
It seems to me QFileSystemModel doesn't remove the file entry from its cache when remove() is called and then creates the index successfully because the entry exists in the cache although the file itself can't be found (the comment in code says in this case:

// we couldn't find the path element, we create a new node since we
// _know_ that the path is valid

I'd call it a bug and an incorrect assumption :)