6

这是我必须显示名为“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_())

即使我将其设置RootPath为 "C:\Myfolder" ,树视图也会显示所有驱动器和文件夹。

如何限制QFileSystemModelTreeView显示“C:\Myfolder”目录中的项目?

4

1 回答 1

14

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

于 2013-11-13T08:06:01.210 回答