Skip to content

Instantly share code, notes, and snippets.

@ben-hearn-sb
Created November 21, 2020 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ben-hearn-sb/ab2693db819248efea85ecef355e0887 to your computer and use it in GitHub Desktop.
Save ben-hearn-sb/ab2693db819248efea85ecef355e0887 to your computer and use it in GitHub Desktop.
from PySide2 import QtCore, QtWidgets, QtGui
class Tree(QtWidgets.QTreeView):
def __init__(self):
super(Tree, self).__init__()
model = QtWidgets.QFileSystemModel(self)
model.setReadOnly(True)
skip_exts = ['.wav', '.mp3']
proxy = HideFileTypesProxy(excludes=skip_exts, parent=self)
#proxy = ProxyModel(model)
#proxy.set_exclusion(('name', '.jpg'))
proxy.setDynamicSortFilter(True)
proxy.setSourceModel(model)
self.setModel(proxy)
#root = "/Users/benhearn/Music/tracks_new/2020"
root = '/Users/benhearn/Music/tracks_new/2020/_not_micro/'
idx = model.setRootPath(root)
self.setRootIndex(proxy.mapFromSource(idx))
self._model = model
self._proxy = proxy
self.resize(600, 400)
class HideFileTypesProxy(QtCore.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 not name.endswith(exc):
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment