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. }
Attachments
For Gerrit Dashboard: QTBUG-117383 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
505692,7 | QFile::moveToTrash: fix error reporting on Windows | dev | qt/qtbase | Status: MERGED | +2 | 0 |
506753,2 | QFile::moveToTrash: fix error reporting on Windows | 6.6 | qt/qtbase | Status: MERGED | +2 | 0 |
506869,2 | QFile::moveToTrash: fix error reporting on Windows | 6.5 | qt/qtbase | Status: MERGED | +2 | 0 |
507004,3 | QFile::moveToTrash: fix error reporting on Windows | tqtc/lts-6.2 | qt/tqtc-qtbase | Status: ABANDONED | +2 | 0 |