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. Check how deep folder is compared to other folder
Qt 6.11 is out! See what's new in the release blog

Check how deep folder is compared to other folder

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.0k Views 1 Watching
  • 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.
  • E Offline
    E Offline
    Eligijus
    wrote on last edited by
    #1

    Hello,

    Lets say I have base folder /test
    I wonder if there is something standard in Qt that would let me know how deep I am from base folder.
    I.e. My other folder is /test/sample/end and comparing to base folder it would return 2 then /test/sample would return 1 and /test would return 0.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Try:

      int countDepth(QDir directory){
      int result;
      for(result=0;directory.cdUp();++result){}
      return result;
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      E 1 Reply Last reply
      3
      • VRoninV VRonin

        Try:

        int countDepth(QDir directory){
        int result;
        for(result=0;directory.cdUp();++result){}
        return result;
        }
        
        E Offline
        E Offline
        Eligijus
        wrote on last edited by
        #3

        @VRonin I have tried it and added check to see if folder includes base folder

        QDir basePath("/home/something/test");
        QDir t("/home/something/test/sample/end");
        QDirIterator it(basePath, QDirIterator::Subdirectories);
        
        bool isBase = false;
        
        while (it.hasNext()) {
            if (it.next() == t.path()) {
                isBase = true;
                break;
            }
        }
        
        int i = 0;
        if (isBase) {
            while (basePath.path() != t.path()) {
                t.cdUp();
                i++;
            }
        } else {
            i = - 1;
        }
        
        qDebug() << i;
        
        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          if t is not a subdirectory of basePath this is an infinite loop

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          E 1 Reply Last reply
          1
          • VRoninV VRonin

            if t is not a subdirectory of basePath this is an infinite loop

            E Offline
            E Offline
            Eligijus
            wrote on last edited by
            #5

            @VRonin Huh? I'm checking with QDirIterator if t is a subdirectory of basePath and setting isBase then only if isBase is true i'm going into loop. I have tested and it's working as intended or am I missing something here?

            1 Reply Last reply
            1

            • Login

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