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:
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
How to get the permissions of a specific file?
To write text, we can use operator<<(), which is overloaded to take a QTextStream on the left and various data types (including QString) on the right: QFile file("out. txt"); if (! file.
QFile is an I/O device for reading and writing text and binary files and resources. A QFile may be used by itself or, more conveniently, with a QTextStream or QDataStream. The file name is usually passed in the constructor, but it can be set at any time using setFileName().
From Qt doc. about QTextStream::operator<<(const QString &string) : Writes the string string to the stream, and returns a reference to the QTextStream. The string is first encoded using the assigned codec (the default codec is QTextCodec::codecForLocale()) before it is written to the stream.
For the loading feature, we also obtain fileName using QFileDialog::getOpenFileName(). This function, the counterpart to QFileDialog::getSaveFileName(), also pops up the modal file dialog and allows the user to enter a file name or select any existing . abk file to load it into the address book.
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)
{
}
As stated by the compiler error you got, you were passing a QIODevice::OpenModeFlag
enum value to setPermissions()
, the QIODevice::OpenModeFlag
is used to describe the mode in which a device is opened.
What you need here is a combination of values from the QFileDevice::Permission enum.
QFile(path).setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With