Details
Description
If you use QFile::moveToTrash on a file that is still opened by another QFile then it will return true but set the fileName to a null string and fail to recycle the file. I would expect this to work as opening a text file and then deleting via file explorer will move the file to the trash. At the very least it should return false to indicate that it failed to do what was requested.
void FileSystemOperationsTest::qtBugTest() { // Simple test directory to write test file to. QDir testDir{"C:/Users/<username>/Desktop/"}; // Create an empty test file with some contents. QString filename{"test.txt"}; QFile file(testDir.absoluteFilePath(filename)); QVERIFY2(file.open(QIODevice::ReadWrite), "Unable to open test file."); file.write("hello world"); file.close(); // Check that the file now exists. QFileInfo fileInfo{file}; QVERIFY(fileInfo.exists()); // Check that the file is not empty. qint64 originalSize{fileInfo.size()}; QCOMPARE_NE(originalSize, 0); // Open the file with a different QFile <- This is important as it will work // if you open with the same QFile as moveToTrash closes the QFile internally. QFile file2(testDir.absoluteFilePath(filename)); QVERIFY2(file2.open(QIODevice::ReadWrite), "Unable to open test file."); // Try to move the file to trash. bool suc = file.moveToTrash(); Q_ASSERT(suc); // This is True Q_ASSERT(!file.fileName().isNull()); // But this fails and the file still exists. }