Results 1 to 4 of 4

Thread: valid a path name

  1. #1
    Join Date
    Jan 2006
    Location
    Stuttgart
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default valid a path name

    When a user enters a non valid path name for a folder (for example containing not allowed characters) so that I can not create the path via QDir().mkpath(pathname), qt tells me via the false return that this was not possible.
    Is there a way allready included in Qt to tell the user why he can not create the folder? (invalid characters in path name, target write protected...)
    Klaus

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: valid a path name

    Hi Klaus!

    You'd have to do all the checks yourself, before calling mkPath(). I think you'd have to iterate or recurse through deeper and deeper components of the path and do the checks using QFileInfo or something like that. Remember that mkPath can create multiple directories (if parents to the target directory don't exist), so it might be wiser to use mkDir() instead and create all those dirs yourself one at a time.

    A snippet that more or less should do it:

    Qt Code:
    1. QString pathtocreate; // (absolute)
    2. QStringList components = pathtocreate.split('/');
    3. QDir dir("/");
    4. foreach(QString component, components){
    5. if(dir.exists(component)){
    6. dir.cd(component);
    7. } else {
    8. QFileInfo finfo(dir, component);
    9. // validate path
    10. // ...
    11. // for example:
    12. // if(!finfo.isWritable()) return QString("Directory not writable");
    13. bool r = dir.mkDir(component);
    14. if(!r) return QString("Could not create directory %1").arg(dir.absoluteFilePath(component));
    15. r = dir.cd(component);
    16. if(!r) return QString("Directory %1 not accessible").arg(dir.absoluteFilePath(component));
    17.  
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

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

    klaus1111 (23rd July 2006)

  4. #3
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: valid a path name

    I suppose you'd better use QDir::separator() instead of '/'.
    Qt Code:
    1. QStringList components = pathtocreate.split(QDir::separator());
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: valid a path name

    Quote Originally Posted by Cesar
    I suppose you'd better use QDir::separator() instead of '/'.
    That's part of the "more or less" statement

Similar Threads

  1. Replies: 3
    Last Post: 25th August 2010, 12:39
  2. qtreeview -file path
    By onder in forum Newbie
    Replies: 3
    Last Post: 12th July 2006, 09:40
  3. Which path should I use?
    By Dark_Tower in forum Newbie
    Replies: 3
    Last Post: 8th April 2006, 12:09
  4. MinGW Windows Library Search Path
    By zztop in forum Qt Programming
    Replies: 4
    Last Post: 26th March 2006, 15:18
  5. Set include file path in KDevelop
    By zlatko in forum KDE Forum
    Replies: 2
    Last Post: 16th January 2006, 11:12

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.