PyQt获取系统文件图标

其实也就3行代码

fileInfo = Qt.QFileInfo(filename)

fileIcon = Qt.QFileIconProvider()

icon = QtGui.QIcon(fileIcon.icon(fileInfo))

下面是一个小例子:

 

   
   
   
   
# !/usr/bin/env python # -*- coding:utf-8 -*- from PyQt4 import QtGui, Qt, QtCore class GetIconDemo(QtGui.QPushButton): def __init__ (self): super(GetIconDemo, self). __init__ () self.setWindowTitle(u " 获取图标 " ) self.setIconSize(Qt.QSize( 50 , 50 )) self.clicked.connect(self.getIconClick) def getIconClick(self): filename = unicode(QtGui.QFileDialog.getOpenFileName(self, u " 获取文件... " , '' , u ' 所有类型(*) ' )) if filename: icon,name = self.getFileInfo(filename) self.setIcon(icon) self.setText(name) def getFileInfo(self,filename): # 获取文件的图片和名字 fileInfo = Qt.QFileInfo(filename) fileIcon = Qt.QFileIconProvider() icon = QtGui.QIcon(fileIcon.icon(fileInfo)) name = QtCore.QFileInfo(filename).fileName() return icon,name if __name__ == ' __main__ ' : import sys app = QtGui.QApplication(sys.argv) frm = GetIconDemo() frm.show() sys.exit(app.exec_()) 运行效果如下:

123 win123

你可能感兴趣的:(PyQt获取系统文件图标)