How to make QTreeView/QListView display specific folders?



  • i want to make a custom file manager that just shows specific folders.
    like this screenshot: only normal folders are displayed in the treeView, and only special folders ending with .asset (or ANY folder with a specific name such as beginning with an underscore "_folder9") are diaplayed in the listView.
    alt text

    filtering in QTreeView/QListView? or filtering in QFileSystemModel? QSortFilterProxyModel? even customize a QAbstractItemModel? which is the best solution? AT LEAST, it works!



  • Well as I denoted to you elsewhere you could handle this internally yourself. Further since you are wanting to make a custom file manager why not do just that as follows:

    Get a complete listing for the Folders and Files:
    https://www.mkyong.com/python/python-how-to-list-all-files-in-a-directory/

    Their For Loop:

    for r, d, f in os.walk(path):
    

    could be written a bit more clearly by denoting it as follows:

    for Folder, FolderList, FileList in os.walk(path):
    

    Where path is your starting folder such as maybe 'C:' or 'C:\Diretory1\SubDirectory1-2'

    Then translate this into your own cascading Dictionary structure

    { Folder : {Folder : File} 
      ...
      Folder : File} }
    

    With this dictionary of just the essentials that you need you can now either limit it when you read it in to only include what it is you want it to include --or-- you can parse it down into sub-dictionaries after getting everything but prior to displaying. It all greatly depend on what you needed to do with that information after displaying that would dictate how you go about holding on to it for quick referencing.

    This of course all happens prior to getting to your TreeView and/or ListView -- Ideally within your own self-contained Class where all you give to the Gui is the relevant TreeView Data in a format that facilitates being displayed within a QTreeView and your ListView Data in a format that facilitates being displayed within a QListView.

    This way you have absolute control of everything going in and coming out.

    Or you could attempt to sub-class one or more of those objects you listed but you being a newbie you might want to just keep it simple and dive into that can of wyrms once you have a better understanding of basic coding concepts. If you code the above in your own class encapsulating all the folder/file manipulation stuff -- you could design it such that you could pass in your own filtering elements and create a completely different out put that you then pass directly (or semi-directly) to your Views.

    Sometimes it is best to start small and work up, take smaller bites out of the project build it in stages.

    If you would like a programming tutor/mentor I do it for free to help young programmers get a quality understanding of coding principles and if you are interested in learning fish rather than just getting a fish just pm me and will send you and invite to the Discord message server I use for tutoring/mentoring/collaborating



  • @denni-0 suppose i read file list in pure python, but how do i pass the data to QTreeView/QListView??



  • You right a function like the following:

    def SetContent(self, tmpItmList):
            self.model.setRowCount(0)
    
            idx = 0
            for Item in tmpItmList:
              # Required to avoid getting a duplicate data insert message
                idx += 1
                blnkVal = QStandardItem(str(idx))
    
                ItmNam = QStandardItem(Item['ItemName'])
                ItmNam.setTextAlignment(Qt.AlignLeft)
                ItmNam.isEditable = False
    
                ItmVal = QStandardItem(Item['Value'])
                ItmVal.setTextAlignment(Qt.AlignCenter)
    
                ItmUnt = QStandardItem((Item['Units']))
                ItmUnt.setTextAlignment(Qt.AlignLeft)
                ItmUnt.isEditable = False
    
                self.model.appendRow([blnkVal, ItmNam, ItmVal, ItmUnt])
    

    The above is a method within my QTreeView class that I use for the ListView on the right side -- your version would be dependent on how you ended up storing your data further the left QTreeView would have parents and children so that would have to be coded differently. I have a version of that as well but could not find it quickly and figured with this you ought to be able to figure out how to implement the parent/child version of QTreeView by referencing the documentation and perhaps other posted examples out on the web as that was how I figured it out.


 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.