Search code examples
windowsqtfile-permissions

QFileInfo::isWritable() returning false at full permissions on windows 7


QFile file(filePath);
QFileInfo fileInfo(file);

file.open(QIODevice::ReadWrite); //or WriteOnly

if(!fileInfo.isWritable())
{
    //Log error
}
else
{
    //Save to file
}

Problem is, filepath has full access, file is created and writable and yet isWritable() still return false. If I remove the write access check and simply write to file, writing is successful. QFile::open also returns true. Any ideas?


Solution

  • QFileInfo::isWritable() is not for checking if a open file is writable, it's to check the permission of current user on the file. If all you want is the see if the open file operation succeeded, check the return value from the call QFile::open(). If it returns true then the file is ready to write. Do your error handling if it returns false.

    I don't know why isWritable() fails in your case. It may be that Windows thinks the file has been open thus not writable from another open() call. If you really want to check permission, call isWritable() before opening the file.