I've been beating my head against this for a while now.

Ultimately, I'd like to filter on a substring in the file name.
If I understand correctly, the vanilla QFileDialog will only do file extensions (i.e. the characters after the period, like .exe or .xml)

So I wrote the following in an attempt to "try it".:

Qt Code:
  1. import sys
  2.  
  3. from PyQt5 import Qt, QtCore, QtGui, QtWidgets
  4. from PyQt5.QtGui import QColor, QBrush
  5. from PyQt5.QtGui import QPainter, QColor, QPen, QFont
  6. from PyQt5.QtCore import *
  7. from PyQt5.QtWidgets import *
  8.  
  9. class FileFilterProxyModel(QtCore.QSortFilterProxyModel):
  10. def __init__(self, parent=None):
  11. super(FileFilterProxyModel, self).__init__(parent)
  12.  
  13. def filterAcceptsRow(self, source_row, srcidx):
  14. model = self.sourceModel()
  15. index0 = model.index(source_row, 0, QtCore.QModelIndex())
  16. index1 = model.index(source_row, 1, QtCore.QModelIndex())
  17. index2 = model.index(source_row, 2, QtCore.QModelIndex())
  18. str0 = model.data(index0, Qt.DisplayRole)
  19. str1 = model.data(index1, Qt.DisplayRole)
  20. str2 = model.data(index2, Qt.DisplayRole)
  21. print('{}, data: {}{}{}'.format(source_row, str0, str1, str2))
  22. return True
  23.  
  24. app = QtWidgets.QApplication(sys.argv)
  25. dlg = QFileDialog()
  26. proxymodel = FileFilterProxyModel(dlg)
  27. dlg.setProxyModel(proxymodel)
  28. #dlg.setOption(QFileDialog.DontUseNativeDialog)
  29. dlg.show()
  30. app.exec_()
To copy to clipboard, switch view to plain text mode 

But, I can't seem to figure out how to get the file name to decide whether it has the substring.

"model" is a QFileSystemModel.
The code above returns '/' for str0, blank for str1, and the word 'Drive'.
Tried model.data(index0, QFileSystemModel.FileNameRole) returns nothing
In other versions of this code I tried model.fileName(index) which always returns nothing.

But, it does display the file dialog with all files listed (with return = True in filterAcceptsRow) and no files (return False)

Once again I'm confused...happens to me a lot.