PyQt can show a directory structure using a QTreeView. For the treeview to show as a directory tree, we need to set its model to a QFileSystemModel instance. That is achieved by calling the setModel method for the tree instance.
We can set additional options on the tree object: sorting enabled (setSortingEnabled), animation and indention.
Example The code below will add the directory view (QTreeView combined with QFileSystemModel) to a grid window. A layout needs to be set for the widget to be seen.
The path is specified using the models setRootPath() method, where the parameter is the full path to the directory. By default its the root.
Leave a Reply: