Results 1 to 3 of 3

Thread: Restrict QFileDialog to one specific directory

  1. #1
    Join Date
    Jan 2010
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question Restrict QFileDialog to one specific directory

    Hi,
    how can I restrict the Qt file dialog to only show the files in the given directory (/usr/app/images) and that navigating out of that directory is not possible? (I do not want the user to see any directory navigation possibilities)
    Qt Code:
    1. const QString IMAGE_DIR="/usr/app/images";
    2. ...
    3. QString fileName = QFileDialog::getOpenFileName(
    4. this,
    5. trUtf8("Choose a Picture"),
    6. IMAGE_DIR,
    7. trUtf8("Cool Picture (*.png)")
    8. );
    To copy to clipboard, switch view to plain text mode 

    Thank you for a hint.

    Spikey

  2. #2
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Restrict QFileDialog to one specific directory

    Disclaimer: I have never used the QFileDialog, but, based on what I see, I would try connecting the directory entered signal (see http://doc.qt.nokia.com/4.6/qfiledia...rectoryEntered) to my own slot. It is possible that the other signals (such as currentChanged ) may be useful, like when they try to choose a new directory, just a wild guess.

    Initially, I would print qDebug statements to learn the behavior, but, if my guess is correct, if the directory you are entering is not the one that you desire, then, set the directory back. The last portion may be a bit tricky, but, take a look and see if it might work.

  3. The following user says thank you to pitonyak for this useful post:

    aerik (12th July 2011)

  4. #3
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Restrict QFileDialog to one specific directory

    It's been a while since this thread was active, but I was having the same problem as spikey, and I stumbled on it from Google.

    pitonyak, your idea absolutely worked. Hook a slot up to the directoryEntered signal, and then check whether the new directory startsWith whatever path you want to restrict them to. If so, update an sCurrentDir variable. If not, setDirectory to sCurrentDir.

    One important caveat: You also have to check the final file the user chooses, to make sure it's where you expect. Otherwise, the user can type something like "../../restrictedDir/restrictedFile.txt", and the dialog will even give them a drop-down of file completion to help them! Be aware that users can still see files outside your restricted directory area, too, so if viewing files outside is a concern, this might not be a complete solution for you.

    Also, when you check the final file, make sure you do it with a "/" at the end of your path. Otherwise, instead of something like "/acceptableDir/acceptableFile.txt", the user could manually type in a file like "../acceptableDirWhoopsNoItsNot/restrictedFile.txt".

Similar Threads

  1. QFileDialog selecting file or directory
    By ^NyAw^ in forum Qt Programming
    Replies: 5
    Last Post: 13th November 2021, 23:03
  2. Fonts from specific directory
    By praveen_g in forum Newbie
    Replies: 1
    Last Post: 13th November 2009, 10:24
  3. Fonts from specific directory
    By praveen_g in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2009, 10:23
  4. How to restrict directory viewing to a subtree?
    By xfurrier in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2009, 16:29
  5. Replies: 1
    Last Post: 14th October 2007, 23:10

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
  •  
Qt is a trademark of The Qt Company.