Results 1 to 5 of 5

Thread: How to set permissions to a file using QFile::setPermissions()

  1. #1
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile How to set permissions to a file using QFile::setPermissions()

    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:

    Qt Code:
    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);
    To copy to clipboard, switch view to plain text mode 

    But it doesnt work. May you help me find a solution for this. Thank you very much.
    Last edited by g16bit; 17th March 2011 at 14:17.

  2. #2
    Join Date
    May 2010
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set permissions to a file using QFile::setPermissions()

    What is happening is that you are overwriting the permissions not adding to them so what you have here:

    Qt Code:
    1. myfile.setPermissions(QFile::ExeGroup);
    2. myfile.setPermissions(QFile::ExeOther);
    3. myfile.setPermissions(QFile::ExeOwner);
    4. myfile.setPermissions(QFile::ExeUser);
    To copy to clipboard, switch view to plain text mode 

    should look like this:

    Qt Code:
    1. myfile.setPermissions(QFile::ExeGroup | QFile::ExeOther | QFile::ExeOther | QFile::ExeUser);
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Lightbulb Re: How to set permissions to a file using QFile::setPermissions()

    thank you for your supply, but your suggest doesnt work to. I have changed my code like this:
    Qt Code:
    1. QString savefile = QFileDialog::getSaveFileName(this,"Save File", QDir::currentPath(),fillter0) + ".desktop";
    2. QFile myfile(savefile);
    3.  
    4. if(!myfile.setPermissions(QFile::ReadOwner|QFile::WriteOwner|QFile::ExeOwner|QFile::ReadGroup|QFile::ExeGroup|QFile::ReadOther|QFile::ExeOther))
    5. {
    6. qDebug("Something wrong!");
    7. }
    8.  
    9.  
    10. if(!myfile.open(QIODevice::WriteOnly | QIODevice::Text))
    11. return;
    12. QTextStream out(&myfile);
    13. out << preview;
    14. myfile.close();
    To copy to clipboard, switch view to plain text mode 
    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.

  4. #4
    Join Date
    May 2010
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set permissions to a file using QFile::setPermissions()

    Where exactly is it failing? is it prematurely returning or are you getting your qdebug() message saying something is wrong?

  5. The following user says thank you to thenybbler for this useful post:

    g16bit (18th March 2011)

  6. #5
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to set permissions to a file using QFile::setPermissions()

    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.

Similar Threads

  1. Replies: 3
    Last Post: 28th March 2009, 15:37
  2. QFile can't read a file
    By Raccoon29 in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2009, 20:24
  3. Replies: 1
    Last Post: 27th September 2008, 20:12
  4. Replies: 3
    Last Post: 11th September 2008, 20:08
  5. Replies: 1
    Last Post: 20th June 2008, 18:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.