I have set a file to be read-only (right click and check readonly). Now when I try to remove the file using the function bool QDir::remove(const QString & fileName)
the file is not removed and false
is returned.
How do I proceed with this? I have tried fiddling around by changing the permission of the file using QFile::setPermission
, but that returns false
too.
Can anybody advise an approach for the same?

- 826
- 1
- 11
- 27

- 3,800
- 13
- 48
- 78
-
Read section "Platform specific issues" in documentation to `QFile` class. It tells you that the usability of `setPermissions()` on Windows is somewhat limited. To me, it however seems that your problem is that your file is locked because it is in use (by other pending instances of your app or by other program). Therefore you cannot change the permissions. Try using `QFile::error()` to get more info. If that does not help, you can always try to use directly WinAPI. – HiFile.app - best file manager Mar 29 '19 at 11:00
6 Answers
file.setPermissions(QFile::ReadOther | QFile::WriteOther);
file.remove();
should work.

- 311
- 2
- 6
-
1This is the correct answer. If someone else falls into this problem vote it up. I literally lost whole day trying to figure this out. – cen Nov 25 '13 at 19:53
-
It seems that the OP writes that `setPermissions()` returns false... which means he is not able to change the permissions. – HiFile.app - best file manager Mar 29 '19 at 10:55
You can set file permissions with QFile
Of course this only for files you have user permission to do. The error may also be because the file is open in another app

- 94,801
- 28
- 188
- 263
-
Apparently changing permission using QFile::setPermissions also returns false. – Eternal Learner Mar 31 '11 at 18:08
-
Is the file open in something else? Do you have permission to delete it? Are you sure the filename is correct (particularly \ vs / in path) – Martin Beckett Mar 31 '11 at 20:04
First, have you checked QFile::error() to see why the file wasn't removed?
Second, in the event that you're still not getting a useful error message back, you could check the source to find out if you can get more information. Checking the source reveals the following, for example:
QFile::remove() uses the underlying file engine to do the removal. That file engine is platform specific and in qfsfileengine_win.cpp for windows. Line 830 shows that it's using DeleteFile to do the removal so you might be able to get more information by calling GetLastError, though I'd hope that Qt translates the error message appropriately.

- 45,767
- 19
- 102
- 147
-
This does not answer the question at all. He is specifically asking about removing Read Only files which always fails with Qfile::remove(). – cen Nov 25 '13 at 19:40
QDir::remove() function is not a static function. so you can create QDir with parent file path and call then remove it:
QDir dir(parent's directory);
and then
dir.remove(fileName);

- 63
- 1
- 8