Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved QDir and nameFilters

    General and Desktop
    3
    9
    1477
    Loading More Posts
    • 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.
    • DuBu
      DuBu last edited by

      Hi all,

      I want to retrieve the entries of a directory containing files and folders and I want only those files matching a particular pattern. But when I use QDir::setNameFilters() it also tries to match directory names. How can the nameFilters be applied to files only and not to directories?

      Cheers,
      Stephan

      JonB J.Hilk 2 Replies Last reply Reply Quote 0
      • JonB
        JonB @DuBu last edited by

        @DuBu said in QDir and nameFilters:

        QDir::setNameFilters

        You cannot separate these two at the QDir level. (Nor, for the record, can one do so at the underlying Windows/Linux OS call level, so you're not "losing" anything.

        You either have to make separate calls, or you need to do the name filtering yourself on the returned values. See, for example, https://stackoverflow.com/questions/13051418/set-name-filters-only-on-files-not-on-dirs

        DuBu 1 Reply Last reply Reply Quote 0
        • J.Hilk
          J.Hilk Moderators @DuBu last edited by

          Hi @DuBu ,
          I believe you can simply also set a Filter via setFilter

          This is the Filter for only files.

          QDir dir;
          dir.setFilter(QDir::Files);
          

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

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

          DuBu 1 Reply Last reply Reply Quote 0
          • DuBu
            DuBu @JonB last edited by

            @JonB Ok, too bad. I'll have a look at it. Thanks for the really quick answer!

            1 Reply Last reply Reply Quote 0
            • DuBu
              DuBu @J.Hilk last edited by

              @J.Hilk The filter is set to QDir::AllEntries already.

              J.Hilk 1 Reply Last reply Reply Quote 0
              • J.Hilk
                J.Hilk Moderators @DuBu last edited by

                @DuBu correct AllEntries = Dirs | Files | Drives, and you only want file names, right?
                So change it to QDir::Files

                If you want filenames filtered and all folders, than I would suggest 2 QDirs with different filters that you combine than.

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

                Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

                JonB 1 Reply Last reply Reply Quote 0
                • JonB
                  JonB @J.Hilk last edited by

                  @J.Hilk said in QDir and nameFilters:

                  If you want filenames filtered and all folders, than I would suggest 2 QDirs with different filters that you combine than.

                  That is what he has said he wants. Hence he can either make two separate calls, or one call with no name filtering and do the name filtering himself.

                  1 Reply Last reply Reply Quote 0
                  • DuBu
                    DuBu last edited by

                    So, here's my solution:

                    foreach (const QFileInfo & fileInfo, _dir.entryInfoList(QDir::Dirs)) {
                      _entryInfos.append(fileInfo);
                    }
                    foreach (const QFileInfo & fileInfo, _dir.entryInfoList(_nameFilters, QDir::Files)) {
                      _entryInfos.append(fileInfo);
                    }
                    
                    J.Hilk 1 Reply Last reply Reply Quote 3
                    • J.Hilk
                      J.Hilk Moderators @DuBu last edited by

                      @DuBu said in QDir and nameFilters:

                      So, here's my solution:

                      foreach (const QFileInfo & fileInfo, _dir.entryInfoList(QDir::Dirs)) {
                        _entryInfos.append(fileInfo);
                      }
                      foreach (const QFileInfo & fileInfo, _dir.entryInfoList(_nameFilters, QDir::Files)) {
                        _entryInfos.append(fileInfo);
                      }
                      

                      Seems promising, just one more thing, foreach is technically discarded, I would go with the "new" for(x : y) style;

                      for (const QFileInfo & fileInfo : _dir.entryInfoList(QDir::Dirs)) {
                        _entryInfos.append(fileInfo);
                      }
                      for (const QFileInfo & fileInfo : _dir.entryInfoList(_nameFilters, QDir::Files)) {
                        _entryInfos.append(fileInfo);
                      }
                      

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

                      Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

                      1 Reply Last reply Reply Quote 2
                      • First post
                        Last post