Created
November 8, 2016 10:07
-
-
Save justinfx/dcdef94b2916ddd5f31e9f0921999fae to your computer and use it in GitHub Desktop.
Using proxy model to apply custom file extension filtering to a QFileSystemModel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 | |
[email protected] | |
""" | |
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