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?