1

私は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_()

手伝ってくれてありがとう!

4

1 に答える 1

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_())
于 2013-01-18T10:31:09.870 に答える