forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomIcon.py
82 lines (66 loc) · 2.52 KB
/
CustomIcon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年1月26日
@author: Irony."[讽刺]
@site: https://pyqt5.com , https://github.com/892768447
@email: 892768447@qq.com
@file: FileSystemModel
@description:
"""
import sys
from PyQt5.QtCore import QFileInfo
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QFileSystemModel, QFileIconProvider, QApplication,\
QTreeView
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2018 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
# 图标提供类
class FileIconProvider(QFileIconProvider):
def __init__(self, *args, **kwargs):
super(FileIconProvider, self).__init__(*args, **kwargs)
self.DirIcon = QIcon("Data/icons/folder.png")
self.TxtIcon = QIcon("Data/icons/file.png")
def icon(self, type_info):
'''
:param fileInfo: 参考http://doc.qt.io/qt-5/qfileinfo.html
'''
if isinstance(type_info, QFileInfo):
# 如果type_info是QFileInfo类型则用getInfoIcon来返回图标
return self.getInfoIcon(type_info)
# 如果type_info是QFileIconProvider自身的IconType枚举类型则执行下面的方法
# 这里只能自定义通用的几种类型,参考http://doc.qt.io/qt-5/qfileiconprovider.html#IconType-enum
'''
QFileIconProvider::Computer 0
QFileIconProvider::Desktop 1
QFileIconProvider::Trashcan 2
QFileIconProvider::Network 3
QFileIconProvider::Drive 4
QFileIconProvider::Folder 5
QFileIconProvider::File 6
'''
if type_info == QFileIconProvider.Folder:
# 如果是文件夹
return self.DirIcon
return super(FileIconProvider, self).icon(type_info)
def getInfoIcon(self, type_info):
if type_info.isDir(): # 文件夹
return self.DirIcon
if type_info.isFile() and type_info.suffix() == "txt": # 文件并且是txt
return self.TxtIcon
return super(FileIconProvider, self).icon(type_info)
if __name__ == "__main__":
app = QApplication(sys.argv)
model = QFileSystemModel()
model.setIconProvider(FileIconProvider()) # 设置为自定义的图标提供类
model.setRootPath("")
tree = QTreeView()
tree.setModel(model)
tree.setAnimated(False)
tree.setIndentation(20)
tree.setSortingEnabled(True)
tree.setWindowTitle("Dir View")
tree.resize(640, 480)
tree.show()
sys.exit(app.exec_())