Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-117383

QFile::moveToTrash does nothing but returns true

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P2: Important
    • 6.5.4, 6.6.1, 6.7.0 FF
    • 6.5
    • Core: I/O
    • None
    • Windows
    • 6ca2008d6 (dev), a20cf52e0 (6.6), 31ae499e9 (6.5)

    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

        Activity

          People

            vhilshei Volker Hilsheimer
            haiku Ollie Dawes
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes