2014-02-10 39 views
0

有没有办法在QFileSystemModel中获取所选文件的文件路径?如何通过QFileSystemModel获取选定文件的文件路径的索引?

我找到了函数QString QFileSystemModel::filePath (const QModelIndex & index) const,它返回文件路径。但是如何找到所选文件的索引?

我正在使用PyQT。 我有一个QFileSystemModel创建(称为模型),它显示所有的文件和目录正确。然后,我做了

index = QModelIndex() 
print (self.model.filePath(index)) 

试图打印出所选文件的文件路径。但它没有返回任何内容。任何人都知道我需要为索引做些什么?

在此先感谢!

回答

1

的问题是,你需要从视图中的指标如本例:

from PyQt4 import QtGui as gui, QtCore as core 

app = gui.QApplication([]) 

model = gui.QFileSystemModel() 
model.setRootPath(core.QDir.currentPath()) 

tree = gui.QTreeView() 
tree.setModel(model) 
tree.setRootIndex(model.index(core.QDir.currentPath())) 

def dummy(item): 
    index = tree.currentIndex() 
    print model.filePath(index) 

tree.clicked.connect(dummy) 

tree.show() 

app.exec_() 
+0

的感谢!我跟着这个,把视图与'虚拟'连接起来。尽管如此,它仍然打印出空的路径。任何想法为什么? – user2081853

+0

还有一些其他问题。有效! – user2081853

相关问题