5

I am trying to check if a folder is writable so I can prompt an error dialog. I am trying this:

QFileDevice::Permissions permissions_list = QFile( folderName ).permissions();

    if ( permissions_list && QFileDevice::WriteUser )
    {
    }

but it does not work. It's the same for both writable folders and restricted ones.

azal
  • 1,210
  • 6
  • 23
  • 43

1 Answers1

11

use QFileInfo:

QFileInfo my_dir(folderName);

if(my_dir.isDir() && my_dir.isWritable()){
    // Do something
}

but pay attention to this problem if you're on Windows

bibi
  • 3,671
  • 5
  • 34
  • 50
  • 1
    I am on Windows and the problem still remains unresolved. Isn't there a working cross-platform solution? The file I'm trying to write is too big to wait for it to be cached in memory for writing and then get the error message. I want the check to be done a priori. – azal Feb 10 '16 at 14:13
  • 4
    An dirty option would be to create an empty file, you might use the QTemporaryFile http://doc.qt.io/qt-4.8/qtemporaryfile.html which has the advantage of removing itself once closed – bibi Feb 10 '16 at 15:06
  • It looks like that writable on windows just means you can change it's name. That's why the Destop folder is readonly but you can still create a file inside (http://stackoverflow.com/questions/1089598/incorrect-qfileinfo-permissions-for-user-desktop-on-vista-64) – bibi Feb 10 '16 at 15:13
  • since nothing "clean" works till now, I will try to set the flag for the NTFS system as @bibi suggested and i will let you know – azal Feb 11 '16 at 08:30