0

Let's start by considering this simple snippet:

from PyQt5.Qt import *
from pathlib import Path


class Proxy(QSortFilterProxyModel):

    def __init__(self, parent=None):
        super().__init__(parent)

    def filterAcceptsRow(self, source_row, source_parent):
        return True


class Model(QFileSystemModel):

    def __init__(self, parent=None):
        super().__init__(parent)


class TreeView(QTreeView):

    def __init__(self, parent=None):
        super().__init__(parent)


if __name__ == '__main__':
    path = Path("c:/windows/system32/drivers/etc")
    # print("\n".join([str(p) for p in path.glob("**/*")]))

    app = QApplication([])

    proxy = Proxy()
    proxy.setSourceModel(Model())

    view = TreeView()
    view.setModel(proxy)

    proxy = view.model()
    model = proxy.sourceModel()
    model.setRootPath(QDir.rootPath())
    index = model.index(str(path))
    view.setRootIndex(index)
    # view.setCurrentIndex(index)

    view.show()

    app.exec_()

I'd like to know how to display the whole content of the specified path as well as the path itself in the QTreeView, for instance, the QTreeView would display something like below:

C:\WINDOWS\SYSTEM32\DRIVERS\ETC
│   hosts
│   lmhosts.sam
│   networks
│   protocol
│   services
│
└───BACKUP
        hosts_2018-08-24_18-35-52.txt

I know how I'd be able to display just the folder content but I'd also like to know how to show the root folder (c:\windows\system32\drivers\etc) itself.

Reason why my snippet is subclassing QSortFilterPoxyModel is because I understand that'd be the way to go here but I haven't made it work yet.

Could you explain how to fix the above snippet to accomplish this simple task?

BPL
  • 9,632
  • 9
  • 59
  • 117
  • From what I understand you, you want "C:\WINDOWS\SYSTEM32\DRIVERS\ETC" to be the root of the QTreeView and must be placed with its fullpath, and its subdirectories as Backup only its name, am I correct? – eyllanesc Nov 11 '19 at 22:20
  • @eyllanesc Hi, In my post you can see i've printed the content of `c:\windows\system32\drivers\etc` using the cmd `tree /f` and those are exactly the items I'd like to know how to display on the QTreeView, you can see there is 2 folders + 6 files listed there. At this point I'd know how to list the inner content of that folder (6files + 1folder), which could be listed without using QSortFilterProxyModel at all but that's not what i want... Does it make sense? – BPL Nov 11 '19 at 22:37
  • @eyllanescJust to clarify further, 2 folders {`c:\windows\system32\drivers\etc`, `backup`} and 6 files {`hosts, lmhosts.sam, networks, protocol, services, hosts_2018-08-24_18-35-52.txt`}. But again... that's just a path to serve as example... i've used that particular path as it should be available on many windows versions – BPL Nov 11 '19 at 22:38
  • what is the fullpàth of backup? – eyllanesc Nov 11 '19 at 22:40
  • @eyllanesc This [image](https://dl.dropboxusercontent.com/s/hxzz5oipwd77x64/explorer_2019-11-11_23-42-22.png) will explain it better – BPL Nov 11 '19 at 22:42

0 Answers0