0

In my current program I have a QTreeView that is modeled from QFileSystem as follows:

    self.model= QFileSystemModel()
    self.model.setRootPath('')
    self.model.setFilter(QDir.Dirs | QDir.NoDotAndDotDot | QDir.Files)
    self.model.setNameFilters(["*.txt","*.png", "*.zip","*.csv","*.json","*.xml"])
    self.model.setNameFilterDisables(False)
    self.tree= QTreeView()

this shows the contents of a directory, and it shows me all the zips.

I've also used zipfile to find the contents of all of the zips in said directory as follows:

    os.chdir(current_directory)
    for file in glob.glob("*.zip"):
        zip = zipfile.ZipFile(file)
        dict= {file: zip.namelist()}
        self.dict_of_zips.update(dict)

I'd like to create children in the treeview for each zip file, and have each child be a content of the zip folder, as to display all of the items in the zip within the viewer.

I'm struggling, as it seems QFileSystemModel isn't meant to have children added and such.

Is the only way around this to create my own tree view and populate it myself? Is there a way to transfer the icons and filters to such a model?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    No, you can't do it directly, as it's not intended to work with compressed files. You *could* do it by subclassing and reimplementing the right functions (starting from `hasChildren()`), but honestly I wouldn't risk that path: compressed archives can be very big and require a lot of time to compute their structure, which would result in blocking the UI until the operation is completed, and this wouldn't even be easy (nor reliable) as QFileSystemModel is pretty complex and has a quite delicate mechanism for browsing the system and internally store its contents. – musicamante Feb 15 '21 at 17:49
  • @musicamante Would my other idea be practical at all? - basically using a custom treeview to implement it all? – TrashyPanda45 Feb 15 '21 at 17:57
  • 2
    @TrashyPanda45 On unix, you could use [archivemount](https://www.cybernoia.de/software/archivemount.html) to create a virtual file-system for the archive that would allow you to explore it directly in a QFileSystemModel. I doubt whether anything similar exists on other platforms, though. – ekhumoro Feb 15 '21 at 18:50

0 Answers0