2013-01-17 45 views
1

我是一個初學者與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_() 

感謝您的幫助!

回答

3

像這樣的東西應該爲你工作:

#!/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_()) 
相關問題