Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using proxy model to apply custom file extension filtering to a QFileSystemModel
"""
Example of using a proxy model to apply custom
filtering to a QFileSystemModel
RE: https://groups.google.com/d/topic/python_inside_maya/a2QM_KvgfeI/discussion
Justin Israel
justinisrael@gmail.com
"""
from PySide import QtCore, QtGui
class Tree(QtGui.QTreeView):
def __init__(self):
super(Tree, self).__init__()
model = QtGui.QFileSystemModel(self)
model.setReadOnly(True)
skip_exts = ['.jpg', '.log']
proxy = HideFileTypesProxy(excludes=skip_exts, parent=self)
proxy.setDynamicSortFilter(True)
proxy.setSourceModel(model)
self.setModel(proxy)
idx = model.setRootPath("/")
self.setRootIndex(proxy.mapFromSource(idx))
self._model = model
self._proxy = proxy
self.resize(600,400)
class HideFileTypesProxy(QtGui.QSortFilterProxyModel):
"""
A proxy model that excludes files from the view
that end with the given extension
"""
def __init__(self, excludes, *args, **kwargs):
super(HideFileTypesProxy, self).__init__(*args, **kwargs)
self._excludes = excludes[:]
def filterAcceptsRow(self, srcRow, srcParent):
idx = self.sourceModel().index(srcRow, 0, srcParent)
name = idx.data()
# Can do whatever kind of tests you want here,
# against the name
for exc in self._excludes:
if name.endswith(exc):
return False
return True
if __name__ == '__main__':
app = QtGui.QApplication([])
tree = Tree()
tree.show()
tree.raise_()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.