如何为 QTreeView 项目设置自定义图标相对于它的后缀
How to set custom icons for QTreeView items relative to it's suffix
我正在使用 QTreeView 和 QFileSystemModel class 来显示给定的文件夹结构 path.Now 我需要帮助来根据文件扩展名(即如果文件类型是 * .png 设置图标 pngIcon.svg 或者如果文件类型是 *.mp4 然后将图标设置为 mp4Icon.svg).
这是我的代码和 GUI 的快照。
from PySide2 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.inodTreeView = QtWidgets.QTreeView(self.centralwidget)
self.inodTreeView.setObjectName("inodTreeView")
self.verticalLayout.addWidget(self.inodTreeView)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtWidgets.QApplication.translate("MainWindow", "MainWindow", None, -1))
class inodWindow(QtWidgets.QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
super(inodWindow, self).__init__(parent)
self.setupUi(self)
iModel = QtWidgets.QFileSystemModel()
iModel.setRootPath(QtCore.QDir.rootPath())
self.inodTreeView.setModel(iModel)
self.inodTreeView.setRootIndex(iModel.index('E:/Tech/Code/inode'))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = inodWindow()
w.show()
sys.exit(app.exec_())
您必须对 QFileSystemModel 使用 QFileIconProvider,并覆盖方法图标:
import os
current_dir =os.path.dirname(os.path.realpath(__file__))
class FileIconProvider(QtWidgets.QFileIconProvider):
def icon(self, parameter):
if isinstance(parameter, QtCore.QFileInfo):
info = parameter
if info.suffix() == "png":
return QtGui.QIcon(os.path.join(current_dir, "pngIcon.svg"))
elif info.suffix() == "mp4":
return QtGui.QIcon(os.path.join(current_dir, "mp4Icon.svg"))
return super(FileIconProvider, self).icon(parameter)
iModel = QtWidgets.QFileSystemModel()
iModel.setIconProvider(FileIconProvider())
iModel.setRootPath(QtCore.QDir.rootPath())
我正在使用 QTreeView 和 QFileSystemModel class 来显示给定的文件夹结构 path.Now 我需要帮助来根据文件扩展名(即如果文件类型是 * .png 设置图标 pngIcon.svg 或者如果文件类型是 *.mp4 然后将图标设置为 mp4Icon.svg).
这是我的代码和 GUI 的快照。
from PySide2 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.inodTreeView = QtWidgets.QTreeView(self.centralwidget)
self.inodTreeView.setObjectName("inodTreeView")
self.verticalLayout.addWidget(self.inodTreeView)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtWidgets.QApplication.translate("MainWindow", "MainWindow", None, -1))
class inodWindow(QtWidgets.QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
super(inodWindow, self).__init__(parent)
self.setupUi(self)
iModel = QtWidgets.QFileSystemModel()
iModel.setRootPath(QtCore.QDir.rootPath())
self.inodTreeView.setModel(iModel)
self.inodTreeView.setRootIndex(iModel.index('E:/Tech/Code/inode'))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = inodWindow()
w.show()
sys.exit(app.exec_())
您必须对 QFileSystemModel 使用 QFileIconProvider,并覆盖方法图标:
import os
current_dir =os.path.dirname(os.path.realpath(__file__))
class FileIconProvider(QtWidgets.QFileIconProvider):
def icon(self, parameter):
if isinstance(parameter, QtCore.QFileInfo):
info = parameter
if info.suffix() == "png":
return QtGui.QIcon(os.path.join(current_dir, "pngIcon.svg"))
elif info.suffix() == "mp4":
return QtGui.QIcon(os.path.join(current_dir, "mp4Icon.svg"))
return super(FileIconProvider, self).icon(parameter)
iModel = QtWidgets.QFileSystemModel()
iModel.setIconProvider(FileIconProvider())
iModel.setRootPath(QtCore.QDir.rootPath())