-
 KDE-Apps.org Applications for the KDE-Desktop 
 GTK-Apps.org Applications using the GTK Toolkit 
 GnomeFiles.org Applications for GNOME 
 MeeGo-Central.org Applications for MeeGo 
 CLI-Apps.org Command Line Applications 
 Qt-Apps.org Free Qt Applications 
 Qt-Prop.org Proprietary Qt Applications 
 Maemo-Apps.org Applications for the Maemo Plattform 
 Java-Apps.org Free Java Applications 
 eyeOS-Apps.org Free eyeOS Applications 
 Wine-Apps.org Wine Applications 
 Server-Apps.org Server Applications 
 apps.ownCloud.com ownCloud Applications 
--
-
 KDE-Look.org Artwork for the KDE-Desktop 
 GNOME-Look.org Artwork for the GNOME-Desktop 
 Xfce-Look.org Artwork for the Xfce-Desktop 
 Box-Look.org Artwork for your Windowmanager 
 E17-Stuff.org Artwork for Enlightenment 
 Beryl-Themes.org Artwork for the Beryl Windowmanager 
 Compiz-Themes.org Artwork for the Compiz Windowmanager 
 EDE-Look.org Themes for your EDE Desktop 
--
-
 Debian-Art.org Stuff for Debian 
 Gentoo-Art.org Artwork for Gentoo Linux 
 SUSE-Art.org Artwork for openSUSE 
 Ubuntu-Art.org Artwork for Ubuntu 
 Kubuntu-Art.org Artwork for Kubuntu 
 LinuxMint-Art.org Artwork for Linux Mint 
 Arch-Stuff.org Art And Stuff for Arch Linux 
 Frugalware-Art.org Themes for Frugalware 
 Fedora-Art.org Artwork for Fedora Linux 
 Mandriva-Art.org Artwork for Mandriva Linux 
--
-
 KDE-Files.org Files for KDE Applications 
 OpenTemplate.org Documents for OpenOffice.org
 GIMPStuff.org Files for GIMP
 InkscapeStuff.org Files for Inkscape
 ScribusStuff.org Files for Scribus
 BlenderStuff.org Textures and Objects for Blender
 VLC-Addons.org Themes and Extensions for VLC
--
-
 KDE-Help.org Support for your KDE Desktop 
 GNOME-Help.org Support for your GNOME Desktop 
 Xfce-Help.org Support for your Xfce Desktop 
--
openDesktop.orgopenDesktop.org:   Applications   Artwork   Linux Distributions   Documents    LinuxDaily.com    Linux42.org    OpenSkillz.com   
 
Apps
News
Groups
Knowledge
Events
Forum
People
Jobs
Register
Login

-
- Knowledge base . 
Go back to the overview page to see other entries
Back to overview

1
votes
click to vote up

How to set permissions to a file using QFile


Programming - Mar 17 2011
answered
Question: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:



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

QString savefile = QFileDialog::getSaveFileName(this,"Save File", QDir::currentPath(),fillter0) + ".xyz";
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





Answer:Summery from comments:


Quote:

QString savefile = QFileDialog::getSaveFileName(this,"Save File", QDir::currentPath(),fillter0) + ".desktop";
QFile myfile(savefile);
// first open the file
if(!myfile.open(QIODevice::WriteOnly | QIODevice::Text))
return;
// then set the permissions on the open file by creating a mask of all permissions you want
if(!myfile.setPermissions(QFile::ReadOwner|QFile::WriteOwner|QFile::ExeOwner|QFile::ReadGroup|QFile::ExeGroup|QFile::ReadOther|QFile::ExeOther))
{
// and don't forget to check if some error occured (like trying to change the permissions of an existing file you are not allowed to change)
qDebug("Something wrong!");
}
// write something in the file
QTextStream out(&myfile);
out << preview;
// and of course don't forget to close it
myfile.close();





Edit answer


Back to overview


-

 permissions at the same time

 
 by Aiwendil on: Mar 17 2011
 
Score 63%

Haven't tried it right now but might be still useful:

As far as I understand it all permissions you want need to be "ORed" together and set in a single call of "setPermissions"...so to get what you want it should be something like:

Quote:

myfile.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner | QFile::ReadGroup | QFile::ExeGroup | QFile::ReadOther | QFile::ExeOther);


Also it might be a good idea to check if there was any problem

Quote:

if (!myfile.setPermissions(...))
{
std::cout << "Some problem setting the file permissions occured" << std::endl;
}


Reply to this

-

 Re: permissions at the same time

 
 by g16bit on: Mar 17 2011
 
Score 50%

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.


Reply to this

-

 Re: Re: permissions at the same time

 
 by Aiwendil on: Mar 17 2011
 
Score 63%

Okay...now you made me start gcc ;)

This works for me:

Quote:

QString savefile = "test.tst";

//first create the QFile object
QFile myfile(savefile);
// next write whatever you want and close the file again
if(myfile.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream out(&myfile);
out << "A test";
myfile.close();
}

// and last alter the file permissions
if(!myfile.setPermissions(QFile::ReadOwner|QFile::WriteOwner|QFile::ExeOwner|QFile::ReadGroup|QFile::ExeGroup|QFile::ReadOther|QFile::ExeOther))
{
qDebug("Something wrong!");
}


translated to your code this should be:
Quote:

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

if(!myfile.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream out(&myfile);
out << preview;
myfile.close();
if(!myfile.setPermissions(QFile::ReadOwner|QFile::WriteOwner|QFile::ExeOwner|QFile::ReadGroup|QFile::ExeGroup|QFile::ReadOther|QFile::ExeOther))
{
qDebug("Something wrong!");
}



Sorry...don't ask me why ;) Just noticed that with my example code it didn't work the first time but the second time when the file already existed. So assuming you can only change the permission of an already existing file this way...not in advance to have it right from the start. (Okay...hard to believe it's impossible to write files right from the start with the wanted permissons...but couldn't figure out in the few minutes how to do it)


Reply to this

-

 Re: Re: Re: permissions at the same time

 
 by Aiwendil on: Mar 17 2011
 
Score 63%

okay..short update...It's only needed to open the file before being able to set the permissions. So I think this would be better:

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

if(!myfile.open(QIODevice::WriteOnly | QIODevice::Text))
return;
if(!myfile.setPermissions(QFile::ReadOwner|QFile::WriteOwner|QFile::ExeOwner|QFile::ReadGroup|QFile::ExeGroup|QFile::ReadOther|QFile::ExeOther))
{
qDebug("Something wrong!");
}
QTextStream out(&myfile);
out << preview;
myfile.close();


Reply to this

-

 Re: Re: Re: Re: permissions at the same time

 
 by g16bit on: Mar 18 2011
 
Score 63%

OMG!
Finally I've done it. Thank you very much.
I've changed the position of the IF statement as you did last night. But it didnt work. And you said that you had to do it twice. So I try once again. It is OK now. Thank you very much.


Reply to this

-

 Re: Re: Re: Re: Re: permissions at the same time

 
 by Aiwendil on: Mar 18 2011
 
Score 63%

You are welcome...still not sure why it didn't work for you the first time...the last piece of code I posted made it work for me all the time even if I deleted the created file but I hope it works now as you need it.


Reply to this

Add comment

Do you have a question?
Post your question here





-
-
Do you like or dislike Ubuntu Unity?
 Yes, unity is alien technology!
 It is less confusing than Gnome 3 default, shell.
 Granny thinks it is much more usable than Gnome 2
 Canonical is embarrasing itself with this split project
 Gnome 3 default shell is much better
 I dislike Unity, Gnome 3 default shell is alien technology!
 None of the above, I like the 2Gb for free and Apple alike behavior. Will post a comment instead

resultmore
 
 
 Who we are
Contact
More about us
Frequently Asked Questions
Register
Twitter
Blog
Explore
Apps
Jobs
Knowledge
Events
People
Updates on identi.ca
Updates on Twitter
Facebook App
Content RSS   
Events RSS   

Participate
Groups
Forum
Add App
Public API
About KDE-Apps.org
Legal Notice
Spreadshirt Shop
CafePress Shop
Advertising
Sponsor us
Report Abuse
 

Copyright 2003-2014 KDE-Apps.org Team  
All rights reserved. KDE-Apps.org is not liable for any content or goods on this site.
All contributors are responsible for the lawfulness of their uploads.
KDE and K Desktop Environment are trademarks of KDE e.V.