I have a QFileDialog and I want to filter out all paths that are not in ~/Documents.
Current have
from PyQt5 import QtCore
import os
...
dialog = QtWidgets.QFileDialog(...)
dialog.setDirectory(os.path.expanduser("~/Documents"))
dialog.setProxyModel(MyFilter())
selectedPath = dialog.exec_()
...
class MyFilter(QtCore.QSortFilterProxyModel):
def filterAcceptsRow(self, p_int, QModelIndex):
sourceModel = self.sourceModel()
index = sourceModel.index(p_int, 0,QModelIndex)
path = sourceModel.filePath(index)
return self._inside_documents_or_is_ancestor(path)
def _inside_documents_or_is_ancestor(self, path):
docpath = os.path.expanduser("~/Documents")
if path.startswith(docpath) or docpath.startswith(path):
print True, path, docpath
return True
return False
It seems like none of the paths are being filtered since I can choose any file in my filesystem in the QFileDialog.
I'm not sure about details of filterAcceptsRow(), but it seems that if I reject a directory, its subdirectories will not be considered, that's why I'm accepting those paths that ancestors of my desired path.
I am running pyqt 5.1 and python 2.7.5