Showing files inside a directory with PyQt6 and QDir

178 Views Asked by At

I am trying to show files inside a directory in a ListView-Widget. Now everytime I enter a different path than the rootPath in the self.file_model.index(...) method, the window doesn't show any existing files in that folder. How do I manage to get the list view to show what is in that folder?

    self.file_list = QListView()
    main_layout.addWidget(self.file_list)
    
    self.file_model = QFileSystemModel()
    self.file_model.setFilter(QDir.Filter.Files)

    self.file_list.setModel(self.file_model)
    self.file_list.setRootIndex(self.file_model.index(r"C:\Users\...))

I tried to use dir with a list, but then it only shows the file names as strings. With QDir I tried to find a way to set it to a different directory but that didn't work either.

1

There are 1 best solutions below

0
dinglewringle52 On

Solved it by giving the file_model a rootPath. self.file_model.setRootPath(r"C:\Users\..")