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. QFileSystemModel::setNameFilters
Forum Updated to NodeBB v4.3 + New Features

QFileSystemModel::setNameFilters

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.5k 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    I am trying to use the setNameFilters function from QFileSystemModel. The problem I am running into is there is little documentation on this function. So I am not entirely sure everything it can accept. Does anyone know if it can accept regular expressions as a filter?
    https://doc.qt.io/qt-5/qfilesystemmodel.html#setNameFilters

    I am currently providing *.txt or *.cpp as filters. However, when I do this I can no longer see directories when viewing the model via a view. I need to filter the files in a particular directory, but continue to show all directories. I was hoping I could provide a regular expression that matches directories, but that might be bust as files can exist without extensions. So maybe I need to do this elsewhere. The name filters don't seem to be directory aware. I have a QSortFilterProxyModel elsewhere in my code. I could add filename filtering there.

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • fcarneyF fcarney

      @fcarney said in QFileSystemModel::setNameFilters:

      Does anyone know if it can accept regular expressions as a filter?

      I don't know where I found this, but the answer is no.

      I ended up doing my filtering for filenames in the filterAcceptsRow override in QSortFilterProxyModel. There I check is the filename is a directory first. If not I do a RegularExpression::wildcardToRegularExpression to interpret my wildcards as a regular expression. This allows me to use *.txt to get a regex without having to write the regex.

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

      @fcarney
      Did you come across the solution at https://stackoverflow.com/a/25928189/489865 ? My reading is it claims to do what you want. Did you already do the QDir::AllDirs bit in setFilter(QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);? Having said that, the default claims to be QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs, but that doesn't seem to stop people from saying that is the solution.

      1 Reply Last reply
      0
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #2

        @fcarney said in QFileSystemModel::setNameFilters:

        Does anyone know if it can accept regular expressions as a filter?

        I don't know where I found this, but the answer is no.

        I ended up doing my filtering for filenames in the filterAcceptsRow override in QSortFilterProxyModel. There I check is the filename is a directory first. If not I do a RegularExpression::wildcardToRegularExpression to interpret my wildcards as a regular expression. This allows me to use *.txt to get a regex without having to write the regex.

        C++ is a perfectly valid school of magic.

        JonBJ 1 Reply Last reply
        0
        • fcarneyF fcarney

          @fcarney said in QFileSystemModel::setNameFilters:

          Does anyone know if it can accept regular expressions as a filter?

          I don't know where I found this, but the answer is no.

          I ended up doing my filtering for filenames in the filterAcceptsRow override in QSortFilterProxyModel. There I check is the filename is a directory first. If not I do a RegularExpression::wildcardToRegularExpression to interpret my wildcards as a regular expression. This allows me to use *.txt to get a regex without having to write the regex.

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

          @fcarney
          Did you come across the solution at https://stackoverflow.com/a/25928189/489865 ? My reading is it claims to do what you want. Did you already do the QDir::AllDirs bit in setFilter(QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);? Having said that, the default claims to be QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs, but that doesn't seem to stop people from saying that is the solution.

          1 Reply Last reply
          0
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #4

            @JonB said in QFileSystemModel::setNameFilters:

            Did you come across the solution at https://stackoverflow.com/a/25928189/489865

            No, but it works. Thank you!

            For reference for future generations:
            https://doc.qt.io/qt-5/qdir.html#Filter-enum

            C++ is a perfectly valid school of magic.

            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