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

[Solved, but comments appreciated]QFileDialog greying out non-selected entries



  • When I set up a QFileDialog - either with the easy static functions or with the constructors - to filter only a specific file extension. By default, the (non-native) QFileDialog greys out the not filtered entries.
    Is there a way to make the dialog to hide these entries completely?
    @
    QStringList filters;
    filters << "Module Dll's (_qtmdl.dll)"
    << "Dll's (
    .dll)"
    << "All Files (*)";

    QFileDialog dialog(0,"Select a module DLL file.");

    dialog.setFileMode(QFileDialog::AnyFile);
    dialog.setViewMode(QFileDialog::Detail);
    dialog.setOptions(QFileDialog::HideNameFilterDetails |
    QFileDialog::DontUseNativeDialog );
    dialog.setNameFilters(filters);
    QStringList fileNames;
    if(dialog.exec())
    fileNames = dialog.selectedFiles();
    @

    If I understand it correctly, subclassing the proxy would allow me to influence the filter (like the solution "here.":http://stackoverflow.com/questions/2101100/qfiledialog-filtering-folders - But is there a way that I can completely hide the filtered entries?



  • After a bit of research I found a solution. It is made by accessing the underlying QFileSystemModel via subclassing the QSortFilterProxyModel:
    @
    // this is the first part subclassing
    // the QSortFilterProxyModel
    //
    class FileFilterProxyModel : public QSortFilterProxyModel
    {
    protected:
    virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
    {
    QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());
    fileModel->setNameFilterDisables(false);
    return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
    }
    };@

    @
    // this is further down in the code, setting up
    // and executing the QFileDialog

    QStringList filters;
    filters << "Animal Dll's (_animal.dll)"
    << "Dll's (
    .dll)"
    << "All Files (*)";

    QFileDialog dialog(0,"Select a Animal DLL file.");
    dialog.setFileMode( QFileDialog::AnyFile);
    dialog.setViewMode(QFileDialog::Detail);
    dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::DontUseNativeDialog );
    dialog.setNameFilters(filters);

    FileFilterProxyModel* proxyModel = new FileFilterProxyModel();
    dialog.setProxyModel(proxyModel);

    QStringList fileNames;
    if(dialog.exec())
    fileNames = dialog.selectedFiles();
    @

    There are a few open questions, maybe somebody else has a clue:

    • I'm not sure whether the default sorting of the FileDialog still works. Is there a function that influences sorting?
    • Although it works, I find it pretty awkward to subclass and object-cast just to change a setting in the filter. Is there a more elegant way to do this? Probably instead of implementing filterAcceptsRow(), implementing the constructor?

Log in to reply