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. Correctly create QDir from QFileInfo
Forum Updated to NodeBB v4.3 + New Features

Correctly create QDir from QFileInfo

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

    I loop through subfolders (only folders) and have these subfolders as QFileInfo.
    How do I correctly and portably convert a QFileInfo which I know to be a folder into a QDir object?
    The API of QFileInfo utterly confuses me on this:

    QDir QFileInfo::dir() const
    Returns the path of the object's parent directory as a QDir object.
    Note: The QDir returned always corresponds to the object's parent directory, even if the QFileInfo represents a directory.
    

    So that's not the right one...unfortunately, they don't link to anything that might help.
    How about path() ?

    QString QFileInfo::path() const
    Returns the file's path. This doesn't include the file name.
    

    I don't have a "file", so I assume this isn't it, either.

    QString QFileInfo::absoluteFilePath() const
    Returns an absolute path including the file name.
    

    Again, includes a file name. No mention on how folders are treated.

    QString QFileInfo::absolutePath() const
    Returns a file's path absolute path. This doesn't include the file name.
    

    Again, no mention on how a folder would be treated. Would I get the folder itself, or the parent folder?

    I assume that something like path() or absolutePath() probably works, but it leaves an uneasy feeling because the docs don't say that it will work.

    Please, what have I overlooked?

    sierdzioS 1 Reply Last reply
    0
    • A Asperamanca

      I loop through subfolders (only folders) and have these subfolders as QFileInfo.
      How do I correctly and portably convert a QFileInfo which I know to be a folder into a QDir object?
      The API of QFileInfo utterly confuses me on this:

      QDir QFileInfo::dir() const
      Returns the path of the object's parent directory as a QDir object.
      Note: The QDir returned always corresponds to the object's parent directory, even if the QFileInfo represents a directory.
      

      So that's not the right one...unfortunately, they don't link to anything that might help.
      How about path() ?

      QString QFileInfo::path() const
      Returns the file's path. This doesn't include the file name.
      

      I don't have a "file", so I assume this isn't it, either.

      QString QFileInfo::absoluteFilePath() const
      Returns an absolute path including the file name.
      

      Again, includes a file name. No mention on how folders are treated.

      QString QFileInfo::absolutePath() const
      Returns a file's path absolute path. This doesn't include the file name.
      

      Again, no mention on how a folder would be treated. Would I get the folder itself, or the parent folder?

      I assume that something like path() or absolutePath() probably works, but it leaves an uneasy feeling because the docs don't say that it will work.

      Please, what have I overlooked?

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @Asperamanca in general description, the mystery is resolved:

      The file's type is obtained with isFile(), isDir() and isSymLink().

      So, the last part of the file path can be a file or a dir. Thus:

      How do I correctly and portably convert a QFileInfo which I know to be a folder into a QDir object?

      QString getDirectoryPath() {
        const QFileInfo info(something...);
      
        if (info.isDir()) {
          return info.absoluteFilePath();
        }
      
        if (info.isFile()) {
          return info.absolutePath();
        }
      
        return {};
      }
      

      (Z(:^

      A 1 Reply Last reply
      0
      • JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Asperamanca , @sierdzio
        Am I misunderstanding or doesn't QDir QFileInfo::absoluteDir() const (but not QDir QFileInfo::dir() const) do exactly what you ask for?

        A 1 Reply Last reply
        0
        • JonBJ JonB

          @Asperamanca , @sierdzio
          Am I misunderstanding or doesn't QDir QFileInfo::absoluteDir() const (but not QDir QFileInfo::dir() const) do exactly what you ask for?

          A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          @JonB said in Correctly create QDir from QFileInfo:

          @Asperamanca , @sierdzio
          Am I misunderstanding or doesn't QDir QFileInfo::absoluteDir() const (but not QDir QFileInfo::dir() const) do exactly what you ask for?

          That's the question. Just by reading the documentation, I couldn't say if that would yield the folder itself, or it's parent folder.

          JonBJ 1 Reply Last reply
          0
          • sierdzioS sierdzio

            @Asperamanca in general description, the mystery is resolved:

            The file's type is obtained with isFile(), isDir() and isSymLink().

            So, the last part of the file path can be a file or a dir. Thus:

            How do I correctly and portably convert a QFileInfo which I know to be a folder into a QDir object?

            QString getDirectoryPath() {
              const QFileInfo info(something...);
            
              if (info.isDir()) {
                return info.absoluteFilePath();
              }
            
              if (info.isFile()) {
                return info.absolutePath();
              }
            
              return {};
            }
            
            A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            @sierdzio said in Correctly create QDir from QFileInfo:

            @Asperamanca in general description, the mystery is resolved:

            The file's type is obtained with isFile(), isDir() and isSymLink().

            So, the last part of the file path can be a file or a dir. Thus:

            How do I correctly and portably convert a QFileInfo which I know to be a folder into a QDir object?

            QString getDirectoryPath() {
              const QFileInfo info(something...);
            
              if (info.isDir()) {
                return info.absoluteFilePath();
              }
            
              if (info.isFile()) {
                return info.absolutePath();
              }
            
              return {};
            }
            

            I get the isDir() part.
            But isDir() does not link to absoluteFilePath(), absoluteFilePath() does not mention how it handles a folder, and so on.
            The obvious counterpart to isDir() would be dir(), but that must have tripped up so many people that they explicitly mentioned that it will always return the parent folder, no matter what.

            You are likely right, but I can't see how you got the information from the documentation.

            sierdzioS 1 Reply Last reply
            0
            • A Asperamanca

              @JonB said in Correctly create QDir from QFileInfo:

              @Asperamanca , @sierdzio
              Am I misunderstanding or doesn't QDir QFileInfo::absoluteDir() const (but not QDir QFileInfo::dir() const) do exactly what you ask for?

              That's the question. Just by reading the documentation, I couldn't say if that would yield the folder itself, or it's parent folder.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Asperamanca said in Correctly create QDir from QFileInfo:

              That's the question. Just by reading the documentation, I couldn't say if that would yield the folder itself, or it's parent folder.

              But that's why I showed you the alternative which NOT to use, which says "parent directory". The one I recommend specifically does not say that, so it seems clear to me. I don't understand why you don't try it, in preference to the other way you are trying now....

              A 1 Reply Last reply
              0
              • JonBJ JonB

                @Asperamanca said in Correctly create QDir from QFileInfo:

                That's the question. Just by reading the documentation, I couldn't say if that would yield the folder itself, or it's parent folder.

                But that's why I showed you the alternative which NOT to use, which says "parent directory". The one I recommend specifically does not say that, so it seems clear to me. I don't understand why you don't try it, in preference to the other way you are trying now....

                A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                @JonB I guess the forum is the wrong audience for an API / Documentation discussion. Something somehow works, that needs to be good enough for now.

                SGaistS 1 Reply Last reply
                0
                • A Asperamanca

                  @JonB I guess the forum is the wrong audience for an API / Documentation discussion. Something somehow works, that needs to be good enough for now.

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Asperamanca there's nothing wrong discussing documentation. As it is, you find the documentation confusing so you should check and maybe open an issue on the bug tracker to improve the documentation of the class. Wording might be improved and maybe some more code snippets could be useful.

                  Even better, provide a patch to help fix the situation :-)

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • A Asperamanca

                    @sierdzio said in Correctly create QDir from QFileInfo:

                    @Asperamanca in general description, the mystery is resolved:

                    The file's type is obtained with isFile(), isDir() and isSymLink().

                    So, the last part of the file path can be a file or a dir. Thus:

                    How do I correctly and portably convert a QFileInfo which I know to be a folder into a QDir object?

                    QString getDirectoryPath() {
                      const QFileInfo info(something...);
                    
                      if (info.isDir()) {
                        return info.absoluteFilePath();
                      }
                    
                      if (info.isFile()) {
                        return info.absolutePath();
                      }
                    
                      return {};
                    }
                    

                    I get the isDir() part.
                    But isDir() does not link to absoluteFilePath(), absoluteFilePath() does not mention how it handles a folder, and so on.
                    The obvious counterpart to isDir() would be dir(), but that must have tripped up so many people that they explicitly mentioned that it will always return the parent folder, no matter what.

                    You are likely right, but I can't see how you got the information from the documentation.

                    sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    @Asperamanca said in Correctly create QDir from QFileInfo:

                    @sierdzio said in Correctly create QDir from QFileInfo:

                    @Asperamanca in general description, the mystery is resolved:

                    The file's type is obtained with isFile(), isDir() and isSymLink().

                    So, the last part of the file path can be a file or a dir. Thus:

                    How do I correctly and portably convert a QFileInfo which I know to be a folder into a QDir object?

                    QString getDirectoryPath() {
                      const QFileInfo info(something...);
                    
                      if (info.isDir()) {
                        return info.absoluteFilePath();
                      }
                    
                      if (info.isFile()) {
                        return info.absolutePath();
                      }
                    
                      return {};
                    }
                    

                    I get the isDir() part.
                    But isDir() does not link to absoluteFilePath(), absoluteFilePath() does not mention how it handles a folder, and so on.

                    You are right, this should be improved and explained in all relevant places.

                    You are likely right, but I can't see how you got the information from the documentation.

                    To me, The file's type is obtained with implies that for QFileInfo, the last section of a path is always treated as a file. It's up to the user to check if it really is a "file file" or "directory file".

                    There are more clues to this, like:

                    QFileInfo provides information about a file's name and position (path) in the file system, its access rights and whether it is a directory or symbolic link, etc.

                    And it also makes sense in general: since QDir::entryInfoList() returns a list of files and directories, it is quite clear to me that QFileInfo can point to either a file or a directory. You could argue the class name is wrong, but what the alternative would be? QFileSystemObjectInfo? QFileOrDirectoryInfo? QINodeInfo? Each alternative comes with some downsides, too.

                    (Z(:^

                    A 1 Reply Last reply
                    1
                    • sierdzioS sierdzio

                      @Asperamanca said in Correctly create QDir from QFileInfo:

                      @sierdzio said in Correctly create QDir from QFileInfo:

                      @Asperamanca in general description, the mystery is resolved:

                      The file's type is obtained with isFile(), isDir() and isSymLink().

                      So, the last part of the file path can be a file or a dir. Thus:

                      How do I correctly and portably convert a QFileInfo which I know to be a folder into a QDir object?

                      QString getDirectoryPath() {
                        const QFileInfo info(something...);
                      
                        if (info.isDir()) {
                          return info.absoluteFilePath();
                        }
                      
                        if (info.isFile()) {
                          return info.absolutePath();
                        }
                      
                        return {};
                      }
                      

                      I get the isDir() part.
                      But isDir() does not link to absoluteFilePath(), absoluteFilePath() does not mention how it handles a folder, and so on.

                      You are right, this should be improved and explained in all relevant places.

                      You are likely right, but I can't see how you got the information from the documentation.

                      To me, The file's type is obtained with implies that for QFileInfo, the last section of a path is always treated as a file. It's up to the user to check if it really is a "file file" or "directory file".

                      There are more clues to this, like:

                      QFileInfo provides information about a file's name and position (path) in the file system, its access rights and whether it is a directory or symbolic link, etc.

                      And it also makes sense in general: since QDir::entryInfoList() returns a list of files and directories, it is quite clear to me that QFileInfo can point to either a file or a directory. You could argue the class name is wrong, but what the alternative would be? QFileSystemObjectInfo? QFileOrDirectoryInfo? QINodeInfo? Each alternative comes with some downsides, too.

                      A Offline
                      A Offline
                      Asperamanca
                      wrote on last edited by
                      #10

                      @sierdzio Thanks, that clarifies a lot!

                      kkoehneK 1 Reply Last reply
                      0
                      • A Asperamanca

                        @sierdzio Thanks, that clarifies a lot!

                        kkoehneK Offline
                        kkoehneK Offline
                        kkoehne
                        Moderators
                        wrote on last edited by
                        #11

                        @Asperamanca

                        Took the liberty to create a bug report for this: https://bugreports.qt.io/browse/QTBUG-120688

                        Director R&D, The Qt Company

                        A 1 Reply Last reply
                        4
                        • kkoehneK kkoehne

                          @Asperamanca

                          Took the liberty to create a bug report for this: https://bugreports.qt.io/browse/QTBUG-120688

                          A Offline
                          A Offline
                          Asperamanca
                          wrote on last edited by
                          #12

                          @kkoehne Thanks a lot!

                          1 Reply Last reply
                          0
                          • A Asperamanca has marked this topic as solved on

                          • Login

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