Delete directory unsing QFile::moveToTrash()
-
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?
-
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?
@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. -
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?
@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 ? -
That's not the problem, I can move to trash "D:\test1\test2\test3" but not "D:\test1"
Subdirectory problem of QFileSystemModel ? -
Hence my permissions related question.
You may have readonly access to a top level folder but write access to lower levels one. -
That's not the problem, I can move to trash "D:\test1\test2\test3" but not "D:\test1"
Subdirectory problem of QFileSystemModel ?@Zbigniew-Sch
If @SGaist's hunch does not lead you anywhere....First things first: get rid of
QFileSystemModel(andQTreeView). Can you move the desired to trash now? If so, something inQFileSystemModel; 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 tryerror()/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.
-
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 ?!?!?! -
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.