【问题标题】:PyQt and QtreeView : How get the path from the selected filePyQt 和 QtreeView:如何从所选文件中获取路径
【发布时间】:2013-01-01 08:46:34
【问题描述】:

我是 PyQt 的初学者。全部都在标题中:我不明白如何从所选文件中获取路径(和名称)? 我希望稍后使用此路径更新 QListView。

这是我的脚本:

# -*- 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_()

感谢您的帮助!

【问题讨论】:

    标签: python pyqt qtreeview


    【解决方案1】:

    这样的东西应该适合你:

    #!/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_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      相关资源
      最近更新 更多