Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

qt

qt5

qfile

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?

like image 252
km2442 Avatar asked Jan 06 '16 14:01

km2442


People also ask

How do you write data in a QFile?

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.

What is QFile?

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().

How do I write a QString file?

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.

How do I load a file in Qt?

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.


2 Answers

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)
{
}
like image 145
huysentruitw Avatar answered Sep 22 '22 12:09

huysentruitw


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);
like image 30
arainone Avatar answered Sep 21 '22 12:09

arainone