I need to check if a file is writable, to display a warning message when users try to open a file that is not writable.
I found on the QT forums the following examples to check if a file is writable:
const QFileInfo info(fileName);
if (info.permission(QFile::WriteOwner | QFile::WriteGroup | QFile::WriteUser)) {
qDebug() << "File is writable";
} else {
qDebug() << "Read Only file";
}
// Or even simpler:
if (info.isWritable()) {
qDebug() << "File is writable";
} else {
qDebug() << "Read Only file";
}
But unfortunately the above examples only works if a file has a read-only attribute, like this (this file is simple txt and I marked it as read-only):
I found in QT forums that I should look at the file permissions first. So, as you can see my file is not a read-only file (this is the permission of the real file I'm working).
If I go to the security section of the file's properties, I realize that the file only has permissions to read and execute, and of course not to write.
I tried to get the file permissions with the following code, but it doesn't seem to work.
QFileDevice::Permissions p = QFile(fileName).permissions();
if (p & QFileDevice::ReadOwner)
{
qDebug() << "Read file";
}
if (p & QFileDevice::WriteOwner)
{
qDebug() << "Write file";
}
if (p & QFileDevice::ExeOwner)
{
qDebug() << "Exec file";
}
output:
Read file
Write file
I tried with another variants like writeUser, but I get the same result.
Any idea or suggestion.
I'm using Windows 10.
Sorry, I can't share the file for testing.