pyqt目录树QTreeView
参考文献:
https://blog.csdn.net/gong_xufei/article/details/89944399
以下代码。安装了就能运行:
from PyQt5.QtWidgets import *
import sys
app = QApplication(sys.argv)
rootPath = r'D:\Data\CTA_CTAData'
model = QFileSystemModel()
model.setRootPath(rootPath)
treeSpace = QTreeView()
treeSpace.setModel(model)
treeSpace.setAnimated(False)
treeSpace.setSortingEnabled(True)
treeSpace.setAlternatingRowColors(True)
treeSpace.show()
sys.exit(app.exec_())
效果如下图
更新,设置根路径
from PyQt5.QtWidgets import * import sys app = QApplication(sys.argv) rootPath = r'D:/' model = QFileSystemModel() model.setRootPath(rootPath) treeSpace = QTreeView() treeSpace.setModel(model) treeSpace.setAnimated(False)#不展示动画 treeSpace.setSortingEnabled(True) treeSpace.setAlternatingRowColors(True)#是否使用交替颜色绘制背景 treeSpace.setRootIndex(model.index(rootPath)) treeSpace.show() sys.exit(app.exec_())