You would need to add view.setRootIndex(model.index("C:\Myfolder"))
according to the QFileSystemModel documentation.
Python PyQt QFileSystemModel Root Path
-
30-07-2022 - |
Question
This is the code i have to display a tree view of a directory called "C:\Myfolder".
import sys
from PyQt4 import QtGui,QtCore
class Myview(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QMainWindow.__init__(self)
model = QtGui.QFileSystemModel()
model.setRootPath('C:\Myfolder')
view = QtGui.QTreeView()
view.setModel(model)
self.setCentralWidget(view)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myview = Myview()
myview.show()
sys.exit(app.exec_())
Even though i set the RootPath
to "C:\Myfolder" , the tree view display all the drives and folder.
How can i limit the QFileSystemModel
so that TreeView
only display items inside "C:\Myfolder" directory?
Solution
OTHER TIPS
It is extremely important that you write "C:/Myfolder"
and NOT "C:\Myfolder"
. Otherwise, it thinks the directory does not exist and will always show you all drives and all folders.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow