PyQt和QtreeView:如何从选定的文件得到路径

问题描述:

我是一个初学者与PyQt。所有内容都在标题中:我不明白我如何才能从所选文件中获取路径(和名称)? 我希望以后用这个路径更新一个QListView。PyQt和QtreeView:如何从选定的文件得到路径

这里我的脚本:

# -*- coding: utf-8 -*- 
from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

import sys 

class MyWidget(QWidget): 
    # SIGNAUX 
    tree_model_indexSig = pyqtSignal(QModelIndex) 

    def __init__(self, parent=None): 
     super(MyWidget, self).__init__(parent) 

     # connect signaux to slots 
     self.tree_model_indexSig.connect(self.indexSlot) 

     self.model = QFileSystemModel() 
     self.model.setRootPath(QDir.rootPath()) 

     ''' GUI ''' 
     # instantiation du treeview et du listview 
     self.treeView = QTreeView(self) 
     self.treeView.setGeometry(QRect(10, 20, 601, 231)) 
     self.treeView.setObjectName("treeView") 

     ''' END GUI ''' 

     self.treeView.setModel(self.model) 
     self.treeView.setRootIndex(self.model.index(QDir.rootPath())) 
     # clicked.CONNECT 
     self.treeView.clicked.connect(self.treeClicked) 
     self.treeView.show() 


    def treeClicked(self, checked=False): 
     # EMIT 
     self.tree_model_indexSig.emit(self.model.index(QDir.rootPath())) 
    # Definition des slots 
    def indexSlot(self, *args): 
     # 1 argument --> ModelIndex 
     path = QDirModel(args[0]).filePath(args[0].currentIndex()) 
     print path 
     ''' 
     How get the path from the selected file ?? 
     ''' 

if __name__=='__main__': 
    app = QApplication(sys.argv) 
    widget = MyWidget() 
    widget.show() 
    app.exec_() 

感谢您的帮助!

像这样的东西应该为你工作:

#!/usr/bin/env python 
#-*- coding:utf-8 -*- 

import sip 
sip.setapi('QString', 2) 
sip.setapi('QVariant', 2) 

from PyQt4 import QtCore, QtGui 

class MyWindow(QtGui.QWidget): 
    def __init__(self, parent=None): 
     super(MyWindow, self).__init__(parent) 

     self.pathRoot = QtCore.QDir.rootPath() 

     self.model = QtGui.QFileSystemModel(self) 
     self.model.setRootPath(self.pathRoot) 

     self.indexRoot = self.model.index(self.model.rootPath()) 

     self.treeView = QtGui.QTreeView(self) 
     self.treeView.setModel(self.model) 
     self.treeView.setRootIndex(self.indexRoot) 
     self.treeView.clicked.connect(self.on_treeView_clicked) 

     self.labelFileName = QtGui.QLabel(self) 
     self.labelFileName.setText("File Name:") 

     self.lineEditFileName = QtGui.QLineEdit(self) 

     self.labelFilePath = QtGui.QLabel(self) 
     self.labelFilePath.setText("File Path:") 

     self.lineEditFilePath = QtGui.QLineEdit(self) 

     self.gridLayout = QtGui.QGridLayout() 
     self.gridLayout.addWidget(self.labelFileName, 0, 0) 
     self.gridLayout.addWidget(self.lineEditFileName, 0, 1) 
     self.gridLayout.addWidget(self.labelFilePath, 1, 0) 
     self.gridLayout.addWidget(self.lineEditFilePath, 1, 1) 

     self.layout = QtGui.QVBoxLayout(self) 
     self.layout.addLayout(self.gridLayout) 
     self.layout.addWidget(self.treeView) 

    @QtCore.pyqtSlot(QtCore.QModelIndex) 
    def on_treeView_clicked(self, index): 
     indexItem = self.model.index(index.row(), 0, index.parent()) 

     fileName = self.model.fileName(indexItem) 
     filePath = self.model.filePath(indexItem) 

     self.lineEditFileName.setText(fileName) 
     self.lineEditFilePath.setText(filePath) 

if __name__ == "__main__": 
    import sys 

    app = QtGui.QApplication(sys.argv) 
    app.setApplicationName('MyWindow') 

    main = MyWindow() 
    main.resize(666, 333) 
    main.move(app.desktop().screen().rect().center() - main.rect().center()) 
    main.show() 

    sys.exit(app.exec_())