Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Delete directory unsing QFile::moveToTrash()

Delete directory unsing QFile::moveToTrash()

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zbigniew-Sch
    wrote on last edited by
    #1

    Hi,

    I'm using QFileSystemModel and QTreeView. I have several subdirectories in the file structure. I want to delete, for example, the D:\test1\test2\test3 directory and use the QFile::moveToTrash() function. Deleting directories without subdirectories works fine, but not with subdirectories. I'm using Windows 11 and Qt 8.9.0. Does anyone have any ideas or similar experience?

    JonBJ SGaistS 2 Replies Last reply
    0
    • Z Zbigniew-Sch

      Hi,

      I'm using QFileSystemModel and QTreeView. I have several subdirectories in the file structure. I want to delete, for example, the D:\test1\test2\test3 directory and use the QFile::moveToTrash() function. Deleting directories without subdirectories works fine, but not with subdirectories. I'm using Windows 11 and Qt 8.9.0. Does anyone have any ideas or similar experience?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Zbigniew-Sch
      Since I would expect it should be OK to move to trash with subdirectories I would make sure no process, including your own, has any of the subdirectories as their current working directory as that would probably prevent it being trashed/moved under Windows.

      1 Reply Last reply
      0
      • Z Zbigniew-Sch

        Hi,

        I'm using QFileSystemModel and QTreeView. I have several subdirectories in the file structure. I want to delete, for example, the D:\test1\test2\test3 directory and use the QFile::moveToTrash() function. Deleting directories without subdirectories works fine, but not with subdirectories. I'm using Windows 11 and Qt 8.9.0. Does anyone have any ideas or similar experience?

        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Zbigniew-Sch hi,

        Based on the documentation of the function, it should work.
        Did you check the permissions of the content of the folder you are trying to remove ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          Zbigniew-Sch
          wrote on last edited by
          #4

          That's not the problem, I can move to trash "D:\test1\test2\test3" but not "D:\test1"
          Subdirectory problem of QFileSystemModel ?

          JonBJ 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by SGaist
            #5

            Hence my permissions related question.
            You may have readonly access to a top level folder but write access to lower levels one.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • Z Zbigniew-Sch

              That's not the problem, I can move to trash "D:\test1\test2\test3" but not "D:\test1"
              Subdirectory problem of QFileSystemModel ?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Zbigniew-Sch
              If @SGaist's hunch does not lead you anywhere....

              First things first: get rid of QFileSystemModel (and QTreeView). Can you move the desired to trash now? If so, something in QFileSystemModel; if not, then it's nothing to do with that.

              I mentioned checking current directory. This would also apply if another process (e.g. a Command Prompt) has the desired directory, or any of its descendants, as current working directory --- certainly I think Windows does not allow you to delete such a directory, not sure whether it prevents it being moved to trash.

              Next, when your QFile::moveToTrash() fails although we cannot be sure it will be set (I haven't looked at Qt code) you should try error()/errorString() to see if there is a clue there.

              When you do get failure, try (at that instant, with your Qt program still running) moving the same directory to trash from File Explorer. Success or failure?

              Those are the first things I can think of that I would try.

              1 Reply Last reply
              1
              • Z Offline
                Z Offline
                Zbigniew-Sch
                wrote on last edited by
                #7

                I tested deletion in "main.cpp" at the very beginning of the program startup, and I can delete all directories (with or without subdirectories). As soon as I create and initialize the "QFileSystemModel" instance, I can't delete a directory with subdirectories. I also tested "SHFileOperation()" with the Windows SDK, and the function returns error code 0x78 (security issue).
                I suspect the "QFileSystemModel" object is blocking deletion.
                Deleting directories without subdirectories works correctly ?!?!?!

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  QFileSystemModel loads its content asynchronously so at some point it likely has file handles opened to do its work which could explain what you are experiencing.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved