7

I'm trying to implement a folder viewer to view the structure of a specific path. and this folder view should look like a the tree widget in PyQT , i know that the file dialog can help , but i need to have it inside my main window.
i tried to implement this using QTreeWidget and i used a recursed function to loop inside folders, but this is too slow. since it needs to recurse around huge number of folders. is this the right way to do it? or there is a ready qt solution for this problem.
Check the figure below.


enter image description here

iTayb
  • 12,373
  • 24
  • 81
  • 135
Moayyad Yaghi
  • 3,622
  • 13
  • 51
  • 67

2 Answers2

11

for PyQt5 I did this function :

def load_project_structure(startpath, tree):
    """
    Load Project structure tree
    :param startpath: 
    :param tree: 
    :return: 
    """
    import os
    from PyQt5.QtWidgets import QTreeWidgetItem
    from PyQt5.QtGui import QIcon
    for element in os.listdir(startpath):
        path_info = startpath + "/" + element
        parent_itm = QTreeWidgetItem(tree, [os.path.basename(element)])
        if os.path.isdir(path_info):
            load_project_structure(path_info, parent_itm)
            parent_itm.setIcon(0, QIcon('assets/folder.ico'))
        else:
            parent_itm.setIcon(0, QIcon('assets/file.ico'))

then i call it like this :

 load_project_structure("/your/path/here",projectTreeWidget)

and i have this result : enter image description here

Softmixt
  • 1,658
  • 20
  • 20
  • 1
    I believe you forgot to define `projectTreeWidget` – Jon May 17 '18 at 23:32
  • 2
    Yes sorry projectTreeWidget = self.projectTreeWidget which is a QT widget refference : – Softmixt May 25 '18 at 18:31
  • 1
    Dear @Softmixt how can we get clicked item path from QTreeWidget? – Tayyebi Feb 04 '19 at 07:51
  • 1
    Just found the answer! Thank you. https://github.com/Gordarg/gordarg.github.io/commit/1e39b3befc6cd090881a8c82887c0b09c586ce5a?diff=split – Tayyebi Feb 04 '19 at 08:24
  • 1
    I revive this old answer as I don't catch how to use the missing part ' projectTreeWidget = self.projectTreeWidget'. can you clarify where it should go to make it works please ? – William Eguienta Aug 30 '20 at 10:29
  • 1
    "self.projectTreeWidget" is a reference to "QTreeWidget" component defined in your ui file under tag so the code will be : ` projectTreeWidget = self.projectTreeWidget load_project_structure("/your/path/here",projectTreeWidget) ` – Softmixt Aug 31 '20 at 11:23
7

Use models and views.

"""An example of how to use models and views in PyQt4.
Model/view documentation can be found at
http://doc.qt.nokia.com/latest/model-view-programming.html.
"""
import sys

from PyQt4.QtGui import (QApplication, QColumnView, QFileSystemModel,
                         QSplitter, QTreeView)
from PyQt4.QtCore import QDir, Qt

if __name__ == '__main__':
    app = QApplication(sys.argv)
    # Splitter to show 2 views in same widget easily.
    splitter = QSplitter()
    # The model.
    model = QFileSystemModel()
    # You can setRootPath to any path.
    model.setRootPath(QDir.rootPath())
    # List of views.
    views = []
    for ViewType in (QColumnView, QTreeView):
        # Create the view in the splitter.
        view = ViewType(splitter)
        # Set the model of the view.
        view.setModel(model)
        # Set the root index of the view as the user's home directory.
        view.setRootIndex(model.index(QDir.homePath()))
    # Show the splitter.
    splitter.show()
    # Maximize the splitter.
    splitter.setWindowState(Qt.WindowMaximized)
    # Start the main loop.
    sys.exit(app.exec_())
Artur Gaspar
  • 4,407
  • 1
  • 26
  • 28
  • is it possible to just get the listView of files in a folder (https://stackoverflow.com/questions/50283851/how-to-display-list-of-files-in-a-specified-directory-using-pyqt5) – X-Black... May 11 '18 at 03:06