QFileSystemModel retrieve filepath of clicked file

Viewed 2024

I am trying to create a file explorer where you can look up a file. When found, the user should be able to select the file he want to upload. Therefore I need the path of the selected file.

Here is my current code:

import sys
from PyQt4.QtGui import *

class Explorer(QWidget):
    def __init__(self):
        super(Explorer, self).__init__()

        self.resize(700, 600)
        self.setWindowTitle("File Explorer")
        self.treeView = QTreeView()
        self.fileSystemModel = QFileSystemModel(self.treeView)
        self.fileSystemModel.setReadOnly(True)

        root = self.fileSystemModel.setRootPath("C:")
        self.treeView.setModel(self.fileSystemModel)

        Layout = QVBoxLayout(self)
        Layout.addWidget(self.treeView) 
        self.setLayout(Layout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    fileExplorer = Explorer()
    fileExplorer .show()
    sys.exit(app.exec_())

How can I get the path of the file the user clicked on? Thanks for the help

1 Answers
Related