Reputation: 3334
I'm new to qt and trying to hide some directories in a QTreeView
. I'm trying to hide some folders based on their names using a custom QSortFilterProxy
named CacheFilterProxy
.
I setup the tree view this way:
fileModel = QtGui.QFileSystemModel()
rootIndex = fileModel.setRootPath(rootDir)
fileModel.setFilter(QtCore.QDir.Dirs | QtCore.QDir.NoDotAndDotDot)
fileModel.setNameFilters([patternString])
model = CacheFilterProxy()
model.setSourceModel(fileModel)
self.fileTreeView.setModel(model)
self.fileTreeView.setRootIndex(model.mapFromSource(rootIndex))
self.fileTreeView.clicked.connect(self.selectedFileChanged)
and then, in self.selectedFileChanged
I try to extract fileName and filePath of currently selected item in tree view. Name of the file will be easily retrieved, but retrieving file path causes the whole program to stop working and then quit.
def selectedFileChanged(self, index):
fileModel = self.fileTreeView.model().sourceModel()
indexItem = self.fileTreeView.model().index(index.row(), 0, index.parent())
# this works normal
fileName = fileModel.fileName(indexItem)
# this breaks the entire program
filePath = fileModel.filePath(indexItem)
Upvotes: 0
Views: 78
Reputation: 5556
This seems wrong. Your fileModel
is the source but I think index
is a proxy index. I think you must map it to the source model before using it in the fileModel
.
def selectedFileChanged(self, proxyIndex):
sourceModel = self.fileTreeView.model().sourceModel()
sourceIndex = self.fileTreeView.model().mapToSource(proxyIndex)
sourceIndexCol0 = sourceModel.index(sourceIndex.row(), 0, sourceIndex.parent())
# this works normal
fileName = sourceModel.fileName(sourceIndexCol0)
# this breaks the entire program
filePath = sourceModel.filePath(sourceIndexCol0)
Note that I renamed indexItem
to sourceIndexCol0
as it is an index and not an item. It was a bit confusing at first glance.
I could not test the code above. If it doesn't work, verify that the indexes are valid before using them and check their model class.
Upvotes: 1