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. [Solved, but comments appreciated]QFileDialog greying out non-selected entries

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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?
      1 Reply Last reply
      0

      • Login

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