How to refresh a treeview?

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:

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"""
        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)
        root: Optional[QModelIndex] = self.fileSystemModel.setRootPath(f"{self.proj_path}")
        self.treeView.setDragEnabled(True)  # Allow items to be dragged from this panel


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?

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:

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