Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple File browser / file chooser in Python program with Qt-GUI?

I'm currently trying to implement some kind of file browser / "explorer" into a programme... I'm using Python and PySide in connection with the Qt-window-toolkit. More or less this youtube-video shows the behaviour I want to have at the end. However, this tutorial used C++ as programming language and I haven't been able yet to reason the right python code from the C++ example.

Basically, my problem is to get the right column (file view) showing the content of the folder clicked in the left column (tree-style folder view).

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from PySide import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.resize(600, 600)
        self.fileBrowserWidget = QtGui.QWidget(self)
        self.setCentralWidget(self.fileBrowserWidget)

        self.dirmodel = QtGui.QFileSystemModel()
        # Don't show files, just folders
        self.dirmodel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.AllDirs)
        self.folder_view = QtGui.QTreeView(parent=self);
        self.folder_view.setModel(self.dirmodel)
        self.folder_view.clicked[QtCore.QModelIndex].connect(self.clicked)

        # Don't show columns for size, file type, and last modified
        self.folder_view.setHeaderHidden(True)
        self.folder_view.hideColumn(1)
        self.folder_view.hideColumn(2)
        self.folder_view.hideColumn(3)

        self.selectionModel = self.folder_view.selectionModel()
        self.filemodel = QtGui.QFileSystemModel()
        # Don't show folders, just files
        self.filemodel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
        self.file_view = QtGui.QListView(parent=self);
        self.file_view.setModel(self.filemodel)

        splitter_filebrowser = QtGui.QSplitter()
        splitter_filebrowser.addWidget(self.folder_view)
        splitter_filebrowser.addWidget(self.file_view)
        splitter_filebrowser.setStretchFactor(0,2)
        splitter_filebrowser.setStretchFactor(1,4)

        hbox = QtGui.QHBoxLayout(self.fileBrowserWidget)
        hbox.addWidget(splitter_filebrowser)

    def set_path(self):
        self.dirmodel.setRootPath("")

    def clicked(self, index):
        # get selected path of folder_view
        index = self.selectionModel.currentIndex()
        dir_path = self.dirmodel.filePath(index)
        ###############################################
        # Here's my problem: How do I set the dir_path
        # for the file_view widget / the filemodel?
        ###############################################
        self.filemodel.setRootPath(dir_path)


app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
main.set_path()

sys.exit(app.exec_())

As you can see in my code, I've already tried to use the setRootPath-function... however, that doesn't seem to be the correct one. So I wonder, what I've got to do for getting this to work?

like image 594
mozzbozz Avatar asked Feb 12 '12 18:02

mozzbozz


1 Answers

You need to set the root index to the appropriate one in the file model. You can do this by adding the following line to the end of the clicked() function:

self.file_view.setRootIndex(self.filemodel.index(dir_path))

I was able to figure it out from my experience using Qt in C++. The documentation for Qt in C++ is really quite good if you can figure out how it translates to Python. I was able to figure this out by looking at the QFileSystemModel documentation.

like image 179
Justin Peel Avatar answered Oct 13 '22 10:10

Justin Peel