pythonpyqtpyqt4qfilesystemmodel

Python PyQt QFileSystemModel Root Path


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

  • You would need to add view.setRootIndex(model.index("C:\Myfolder")) according to the QFileSystemModel documentation.