Setting read write permission with QFile().setPermissions()

km2442 picture km2442 · Jan 6, 2016 · Viewed 7.6k times · Source

In my Qt 5.5.1 program I have to change my config files permission from read only to read write... I have 2 questions:

  1. How can I set this permission? I have tried: QFile(path).setPermissions(QFile::ReadWrite); But it throws this compiler error:

    C2664: 'bool QFile::setPermissions(const QString &,QFileDevice::Permissions)' : cannot convert argument 1 from 'QIODevice::OpenModeFlag' to 'QFileDevice::Permissions' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

  2. How to get the permissions of a specific file?

Answer

huysentruitw picture huysentruitw · Jan 6, 2016

The right way is by using the correct enum, pick a value from QFileDevice::Permissions-enum instead (I believe this is a Qt5 change). F.e.:

QFile(path).setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner);

To get the permissions of a file, use the .permissions() method of QFile:

QFileDevice::Permissions p = QFile(path).permissions();

Which returns all the file permissions OR-ed together. So to test if a certain permission is set, you can do something like:

if (p & QFileDevice::ReadOwner)
{
}