PDA

View Full Version : How to set permissions to a file using QFile::setPermissions()



g16bit
17th March 2011, 14:08
Hi, everybody. I have a problem when working with file permissions. When I create a file, its default permissions are -rw-r--r-- and now i want to add execute permission to this file. This mean its permissions must be -rwxr-xr-x. So I used these commands:


QString savefile = QFileDialog::getSaveFileName(this,"Save File", QDir::currentPath(),fillter0) + ".desktop";
QFile myfile(savefile);
myfile.setPermissions(QFile::ExeGroup);
myfile.setPermissions(QFile::ExeOther);
myfile.setPermissions(QFile::ExeOwner);
myfile.setPermissions(QFile::ExeUser);

But it doesnt work. May you help me find a solution for this. Thank you very much.

thenybbler
17th March 2011, 16:16
What is happening is that you are overwriting the permissions not adding to them so what you have here:



myfile.setPermissions(QFile::ExeGroup);
myfile.setPermissions(QFile::ExeOther);
myfile.setPermissions(QFile::ExeOwner);
myfile.setPermissions(QFile::ExeUser);


should look like this:



myfile.setPermissions(QFile::ExeGroup | QFile::ExeOther | QFile::ExeOther | QFile::ExeUser);


so that you apply all of the permissions at the same time.

g16bit
17th March 2011, 18:36
thank you for your supply, but your suggest doesnt work to. I have changed my code like this:

QString savefile = QFileDialog::getSaveFileName(this,"Save File", QDir::currentPath(),fillter0) + ".desktop";
QFile myfile(savefile);

if(!myfile.setPermissions(QFile::ReadOwner|QFile:: WriteOwner|QFile::ExeOwner|QFile::ReadGroup|QFile: :ExeGroup|QFile::ReadOther|QFile::ExeOther))
{
qDebug("Something wrong!");
}


if(!myfile.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream out(&myfile);
out << preview;
myfile.close();
But it doesnt work. So i guess it is not beacause the ORed statement. Could you please show me how?
I read the Qt help, but couldnt find any thing could help.
Thank you very much.

thenybbler
18th March 2011, 01:37
Where exactly is it failing? is it prematurely returning or are you getting your qdebug() message saying something is wrong?

g16bit
18th March 2011, 05:17
Ok. this problem was solved. thank you very much. A friend of mine tell me the wrong in my code. just put the if statement below the open file command. Thanks.