How to refresh a treeview?

Heads up! You've already completed this tutorial.

Cody_Jackson | 2021-06-08 14:01:48 UTC | #1

I have a File Explorer panel (based on a tutorial) that uses a QTreeView and QFileSystemModel to show the contents of a directory. I want to update the view of the directory contents if the user changes the directory.

In other words, the current window looks at a default location. Users are able to change a config file to change the directory the window shows. However, they have to close the GUI and reopen to see the contents of the new directory.

I have spent about a day and a half looking at the documentation, Stack Overflow, and other locations to find some means to refresh the window, but I haven't found anything. It seems like this would be a built-in behaviour for QTreeView but I can't find it; maybe I'm looking for the wrong term.

My code is below:

python
class ProjectExplorerTree(QTreeView):
    """Handles all functionality for the Project Explorer panel.

    Creates context menu for Build, Clean, Register, and Delete OpenCPI assets.
    Provides access to selected assets for use in other methods, such as Set/UnSet
    """
    def __init__(self, main_window, tree: QTreeView, console, rcc, hdl) -> None:
        """Creates the ListView/FileSystemModel for viewing"""
        super().__init__()
        self.treeView = tree
        self.default_dir = main_window.ocpi_path
        self.proj_path = main_window.user_proj_path
        self.console: OutputConsole = console
        self.rcc_target: RccTargets = rcc
        self.hdl_target: HdlTargets = hdl

        self.fileSystemModel = QFileSystemModel(self.treeView)
        self.fileSystemModel.setReadOnly(False)
        root: Optional[QModelIndex] = self.fileSystemModel.setRootPath(f"{self.proj_path}")
        self.treeView.setModel(self.fileSystemModel)
        self.treeView.hideColumn(1)
        self.treeView.hideColumn(2)
        self.treeView.hideColumn(3)
        self.treeView.setRootIndex(root)
        self.treeView.setSelectionMode(self.treeView.ExtendedSelection)
        self.treeView.setRootIsDecorated(True)
        self.treeView.setAlternatingRowColors(True)
        self.treeView.setDragEnabled(True)  # Allow items to be dragged from this panel

        self.treeView.setContextMenuPolicy(Qt.CustomContextMenu)
        self.treeView.customContextMenuRequested.connect(self.context_menu)

I have tried using QFileSystemModel.rootPathChanged(), which didn't seem to work. I also tried using QFileSystemWatcher, which didn't do what I wanted (and it seems to already be incorporated into QFileSystemModel). I even tried using QDir.refresh(), which did nothing.

What am I missing to simply refresh the window when the config file is updated?


Cody_Jackson | 2021-06-08 16:59:37 UTC | #2

I don't know if this is the most correct way, but I was able to do it (manually, at least) by creating a Refresh context menu that implemented the following code:

python
def refresh_view(self):
    new_path = self.fileSystemModel.setRootPath(str(window.user_proj_path))
    self.treeView.setRootIndex(new_path)

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

How to refresh a treeview? was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.