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. How to create a new QDir from an existing QDir + a subfolder?

How to create a new QDir from an existing QDir + a subfolder?

Scheduled Pinned Locked Moved Solved General and Desktop
qdir
6 Posts 3 Posters 3.2k 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.
  • E Offline
    E Offline
    EchoReaper
    wrote on 18 Apr 2019, 02:13 last edited by
    #1

    I have an existing QDir. I want a QDir for a folder inside. E.g.

    void someMethod(QDir input) {
        QDir folderInInput = input.path() + "/SubfolderName";
    
        doStuff(folderInInput);
        ...
    }
    

    Normally, scripting languages will you allow to do something like QDir folderInput = input / "SubfolderName", but Qt doesn't. I could just use what I have above, but I'm not a fan of it because it hardcodes the slash. I could do input.path() + QDir::separator() + "SubfolderName", but that's too verbose. Does Qt have a more desirable way to index a subfolder of an existing QDir?

    J J 2 Replies Last reply 18 Apr 2019, 04:48
    0
    • E EchoReaper
      18 Apr 2019, 02:13

      I have an existing QDir. I want a QDir for a folder inside. E.g.

      void someMethod(QDir input) {
          QDir folderInInput = input.path() + "/SubfolderName";
      
          doStuff(folderInInput);
          ...
      }
      

      Normally, scripting languages will you allow to do something like QDir folderInput = input / "SubfolderName", but Qt doesn't. I could just use what I have above, but I'm not a fan of it because it hardcodes the slash. I could do input.path() + QDir::separator() + "SubfolderName", but that's too verbose. Does Qt have a more desirable way to index a subfolder of an existing QDir?

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 18 Apr 2019, 04:48 last edited by
      #2

      Hi @EchoReaper and welcome

      I'm not entirely sure what you try to do here, as doStuff seems to be a local function, but QDir offers a change directory function
      cdUp to go one directory up and
      cd(folderName) to change to a sub directory

      If that is what you're looking for?


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • E Offline
        E Offline
        EchoReaper
        wrote on 18 Apr 2019, 05:08 last edited by
        #3

        cd kind of does what I want, but I want to create a new QDir rather than modifying an existing one. cd doesn't really make sense because of the variable names e.g.

        QDir getDownloads(QDir userProfile) {
            userProfile.cd("Downloads");
        
            return userProfile;
        }
        

        We say we're returning userProfile, but we're not actually. We've changed the QDir to something else. I could clone the QDir and then change that, but that wouldn't be so elegant either. Preferred:

        QDir getDownloads(QDir userProfile) {
            QDir downloads = userProfile.subfolder("Downloads");
        
            maybeDoStuffWithQDirIfThisIsNotSuchASimpleMethod(downloads);
        
            return downloads;
        }
        
        J 1 Reply Last reply 18 Apr 2019, 05:14
        0
        • E EchoReaper
          18 Apr 2019, 05:08

          cd kind of does what I want, but I want to create a new QDir rather than modifying an existing one. cd doesn't really make sense because of the variable names e.g.

          QDir getDownloads(QDir userProfile) {
              userProfile.cd("Downloads");
          
              return userProfile;
          }
          

          We say we're returning userProfile, but we're not actually. We've changed the QDir to something else. I could clone the QDir and then change that, but that wouldn't be so elegant either. Preferred:

          QDir getDownloads(QDir userProfile) {
              QDir downloads = userProfile.subfolder("Downloads");
          
              maybeDoStuffWithQDirIfThisIsNotSuchASimpleMethod(downloads);
          
              return downloads;
          }
          
          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 18 Apr 2019, 05:14 last edited by
          #4

          @EchoReaper
          Well no, there is no function in the QDir class that returns a new dir, at least not in the way you want it.

          You could use the copy constructor?

          Dir newDir(oldDir).cd("Downloads");


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1
          • E Offline
            E Offline
            EchoReaper
            wrote on 18 Apr 2019, 05:25 last edited by
            #5

            I guess that will do. Thanks!

            1 Reply Last reply
            0
            • E EchoReaper
              18 Apr 2019, 02:13

              I have an existing QDir. I want a QDir for a folder inside. E.g.

              void someMethod(QDir input) {
                  QDir folderInInput = input.path() + "/SubfolderName";
              
                  doStuff(folderInInput);
                  ...
              }
              

              Normally, scripting languages will you allow to do something like QDir folderInput = input / "SubfolderName", but Qt doesn't. I could just use what I have above, but I'm not a fan of it because it hardcodes the slash. I could do input.path() + QDir::separator() + "SubfolderName", but that's too verbose. Does Qt have a more desirable way to index a subfolder of an existing QDir?

              J Offline
              J Offline
              JonB
              wrote on 18 Apr 2019, 07:18 last edited by JonB
              #6

              @EchoReaper said in How to create a new QDir from an existing QDir + a subfolder?:

              Normally, scripting languages will you allow to do something like QDir folderInput = input / "SubfolderName", but Qt doesn't. I could just use what I have above, but I'm not a fan of it because it hardcodes the slash. I could do input.path() + QDir::separator() + "SubfolderName", but that's too verbose. Does Qt have a more desirable way to index a subfolder of an existing QDir?

              I would say the "correct" Qt way of producing the path to a sub-folder, without using / or QDir::separator() the way you have (which isn't great if your starting directory ends with a /, like / or C:/, e.g. when QDir::isRoot() == true), is to use https://doc.qt.io/qt-5/qfileinfo.html#setFile-2, void QFileInfo::setFile(const QDir &dir, const QString &file).

              You/Qt don't know whether the sub-item is a file or directory, that's why it's a QFileInfo method rather than a QDir one. You can then, say, feed QFileInfo::absoluteFilePath() to a new QDir() if you want to access it as a directory.

              If you actually wish to create the sub-directory you could use https://doc.qt.io/qt-5/qdir.html#mkdir or https://doc.qt.io/qt-5/qdir.html#mkpath.

              1 Reply Last reply
              0

              2/6

              18 Apr 2019, 04:48

              topic:navigator.unread, 4
              • Login

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