PyQt和QtreeView:如何从所选fi获取路径

2024-04-29 10:11:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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_()

谢谢你的帮助!


Tags: pathfromimportself路径treemodeldef
1条回答
网友
1楼 · 发布于 2024-04-29 10:11:04

像这样的事情应该对你有用:

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

相关问题 更多 >