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. QDir and nameFilters

QDir and nameFilters

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 2.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.
  • DuBuD Offline
    DuBuD Offline
    DuBu
    wrote on last edited by
    #1

    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

    JonBJ J.HilkJ 2 Replies Last reply
    0
    • DuBuD DuBu

      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

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

      @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

      DuBuD 1 Reply Last reply
      0
      • DuBuD DuBu

        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

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        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


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

        DuBuD 1 Reply Last reply
        0
        • JonBJ JonB

          @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

          DuBuD Offline
          DuBuD Offline
          DuBu
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            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);
            
            DuBuD Offline
            DuBuD Offline
            DuBu
            wrote on last edited by
            #5

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

            J.HilkJ 1 Reply Last reply
            0
            • DuBuD DuBu

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

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @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


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

              JonBJ 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @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.

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

                @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
                0
                • DuBuD Offline
                  DuBuD Offline
                  DuBu
                  wrote on last edited by
                  #8

                  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.HilkJ 1 Reply Last reply
                  3
                  • DuBuD DuBu

                    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.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @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


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

                    1 Reply Last reply
                    2

                    • Login

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