[PyQt] QTreeView filtering with QSortFilterProxyModel

JediAce wesleylulee at gmail.com
Fri Mar 4 22:52:35 GMT 2011


Hi Everyone

So I'm trying to filter a QFileSystemModel using QSortFilterProxyModel and
displaying it with QTreeView. The filtering works fine, however, I'm having
trouble setting the root index to the folder I want. To set the root index
for QTreeView, I use:

someTreeView.setRootIndex(
someFilterProxyModel.mapFromSource(indexFromSource))

after some debugging I realized that the index outputted by

someFIlterProxyModel.mapFromSource(indexFromSource) is invalid. Here is some
sample code:


154         # Add file browser
155         model = QFileSystemModel()
156         testDir = QString(dir)
157         model.setRootPath(testDir)
158         self._fsModel = model
159         self._treeView = QTreeView()
160 
161         if self.selectedFileFilter == 'All Files':
162             self._treeView.setModel(model)
163             self._treeView.setRootIndex(model.index(testDir))
164         else:
165             filteredModel = FileSystemFilter()
166             filteredModel.setSourceModel(self._fsModel)
167             ext = self.selectedFileFilter.split(' ')[0]
168             pattern = '*' + ext
169             print 'pattern: %s' % pattern
171             filteredModel.setFilterWildcard(QString('*.spy'))
172             filteredModel.setDynamicSortFilter(True)
175             self._treeView.setModel(filteredModel)
176             print 'model index is valid: %s' %
model.index(testDir).isValid()
177             print 'proxy index is valid: %s' %
filteredModel.mapFromSource(model.index(testDir)).isValid()
179            
self._treeView.setRootIndex(filteredModel.mapFromSource(model.index(testDir)))

Sorry the lines are off. I deleted some comments. Does anyone know why this
is happening? Is this a known bug? Thanks.

-- 
View this message in context: http://old.nabble.com/QTreeView-filtering-with-QSortFilterProxyModel-tp31062355p31062355.html
Sent from the PyQt mailing list archive at Nabble.com.



More information about the PyQt mailing list