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

I'm currently trying to inject some kind of file browser / "explorer" into the program ... I use Python and PySide in connection with the Qt-window-toolkit. More or less, this youtube-video shows the behavior that I want to have in the end. However, this tutorial used C ++ as a programming language, and I still could not explain the correct python code using C ++ as an example.

Basically, my problem is to get the right column (file) displaying the contents of the folder clicked in the left column (tree-style 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 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 already tried to use the setRootPath function ... however, this does not seem to be correct. So I wonder, what do I need to do to get this to work?

+4
source share
2 answers

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

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

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

+4
source

You need to set the root index of the file list view:

 def clicked(self, index): # the signal passes the index of the clicked item dir_path = self.filemodel.filePath(index) root_index = self.filemodel.setRootPath(dir_path) self.file_view.setRootIndex(root_index) 
+2
source

Source: https://habr.com/ru/post/1396060/


All Articles