Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Split file system path (QString) into folder hierarchy
Forum Update on Monday, May 27th 2025

Split file system path (QString) into folder hierarchy

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 6.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on 9 Apr 2015, 10:48 last edited by Violet Giraffe 4 Sept 2015, 10:49
    #1

    Here's what I want to do. I have a path, e. g.
    C:/Users/Admin/AppData/Temp/
    What I want to get is the list of my path's parent folder, that folder's parent folder and so on up to the root entry:
    C:/Users/Admin/AppData/
    C:/Users/Admin/
    C:/Users/
    C:/

    For added fun, this is for a cross-platform app that runs both on Windows and POSIX systems (Linux / Mac OS X).

    I'm tempted to simply split the path using '/' for separator, but I'm afraid that will not be a universal solution and may fail on some weird paths. Windows UNC path (starts with "\?") is one thing that comes to mind, and I'm sure there are other special cases I'm not aware of.

    Another idea I've had is QDir(path).cdUp(), but the problem with that is that cdUp returns false and does nothing if the target folder doesn't exist. And for my application I need this to work for non-existing folders just as well.

    So, is there a Qt way to do what I want? If not and I must implement it all myself, can you recall any other special cases I'd have to handle?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 9 Apr 2015, 11:31 last edited by Chris Kawa 4 Sept 2015, 11:35
      #2

      QFileinfo might be of use here. Something like this should work (or at least be a good starting point):

      QStringList directoryList(const QString& str) {
          auto path = QDir::cleanPath(str);
          QStringList result(path);
          while(!QFileInfo(path).isRoot())
              result << (path = QFileInfo(path).path());
          return result;
      }
      
      V 1 Reply Last reply 9 Apr 2015, 12:00
      2
      • D Offline
        D Offline
        dheerendra
        Qt Champions 2022
        wrote on 9 Apr 2015, 11:32 last edited by
        #3

        I don't think any direct function/method exist like this.
        May be you can try with QDir::Seperator().. and QString split to make it work across the platform ?

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        4
        • C Chris Kawa
          9 Apr 2015, 11:31

          QFileinfo might be of use here. Something like this should work (or at least be a good starting point):

          QStringList directoryList(const QString& str) {
              auto path = QDir::cleanPath(str);
              QStringList result(path);
              while(!QFileInfo(path).isRoot())
                  result << (path = QFileInfo(path).path());
              return result;
          }
          
          V Offline
          V Offline
          Violet Giraffe
          wrote on 9 Apr 2015, 12:00 last edited by
          #4

          Neat trick, Chris, thanks! I didn't think of fooling QFileInfo into treating folder name like a file name and using path().
          Is there a slightest possibility that isRoot() will never be true in this algorithm (assuming any garbage input is theoretically possible)? It looks a little dangerous. I should probably also check path for isEmpty() in the loop condition?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 9 Apr 2015, 12:11 last edited by
            #5

            Yeah, that's why this was a starting point. isRoot() sure wouldn't work for garbage or even something credible like http://....
            A second thought would be to check instead if we managed to peel something off of the path, e.g.

            QStringList foo(const QString& str) {
                auto path = QDir::cleanPath(str);
                QStringList result(path);
                while((path = QFileInfo(path).path()).length() < result.last().length())
                    result << path;
                return result;
            }
            

            This way we guarantee it won't fall into infinite loop. This should also handle garbage reasonably.

            As for the trick - it's not really a trick. It's actually documented in the dir method but (maybe through omission) is just not mentioned for path, which is a shame.

            1 Reply Last reply
            2

            4/5

            9 Apr 2015, 12:00

            • Login

            • Login or register to search.
            4 out of 5
            • First post
              4/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved