2016-05-25 10 views
0

Qtの新機能で、一部のディレクトリをQTreeViewに隠そうとしています。私はCacheFilterProxyという名前のカスタムQSortFilterProxyを使って、名前に基づいていくつかのフォルダを隠そうとしています。カスタムQSortFilterProxyにQFileSystemModelを使用できません

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) 

、その後、self.selectedFileChangedに私は、ツリービューで現在選択されている項目のファイル名やファイルパスを指定して抽出しよう:ツリーがこのように表示

Iセットアップ。ファイルの名前は簡単に取得できますが、ファイルパスを取得するとプログラム全体が停止して終了します。

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) 

答えて

1

これは間違っています。あなたのfileModelがソースですが、私はindexがプロキシインデックスだと思います。 fileModelで使用する前にソースモデルにマップする必要があると思います。それがインデックスではなくアイテムであるとして、私はsourceIndexCol0indexItemに改名

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) 

注意。一見するとちょっと混乱しました。

私は上記のコードをテストできませんでした。動作しない場合は、使用する前にインデックスが有効であることを確認し、モデルクラスを確認してください。

+0

まあ、それはかなり明白に思えました、私はそれを(私はフィルター法で同じことをしたために)起こしたと思っていましたが、私はそれをやることを忘れてしまった。以前はファイル名の取得がうまくいきました。 – Mehraban

関連する問題