Python PyQt QFileSystemModel Root Path

给你一囗甜甜゛ 提交于 2019-11-28 07:55:40

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/19948159/python-pyqt-qfilesystemmodel-root-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!