Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QFileSystemModel::setNameFilters



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



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



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



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



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


Log in to reply