Qt中获取系统图标、类型是通过QFileIconProvider来实现的,具体如下:
一、Qt获取系统文件图标1、获取文件夹图标QFileIconProvider icon_provider;QIcon icon = icon_provider.icon(QFileIconProvider::Folder);2、获取指定文件图标QFileInfo file_info(name);
QFileIconProvider icon_provider;
QIcon icon = icon_provider.icon(file_info);二、Windows编程中获取系统图标、类型:QIcon Setting::getFileIcon(QString file_name)
{QIcon file_icon = QIcon(":/icon/file");if(!file_name.isEmpty()){//获取后缀std::string str = "file";int index = file_name.lastIndexOf(".");if(index >= 0){QString suffix = file_name.mid(index); //将QString转化为std::stringstr = suffix.toUtf8().constData(); //std::string((const char *)path.toLocal8Bit()) path.toLatin1().data();}LPCSTR name = str.c_str();SHFILEINFOA info;if(SHGetFileInfoA(name,FILE_ATTRIBUTE_NORMAL,&info,sizeof(info),SHGFI_SYSICONINDEX | SHGFI_ICON | SHGFI_USEFILEATTRIBUTES)){HICON icon = info.hIcon;file_icon = QIcon(QPixmap::fromWinHICON(icon));}}return file_icon;
}QString Setting::getFileType(QString file_name)
{QString file_type = QString("file");if(!file_name.isEmpty()){//获取后缀std::string str = "file";int index = file_name.lastIndexOf(".");if(index >= 0){QString suffix = file_name.mid(index); //将QString转化为std::stringstr = suffix.toUtf8().constData();}LPCSTR name = str.c_str();SHFILEINFOA info;if(SHGetFileInfoA(name,FILE_ATTRIBUTE_NORMAL,&info,sizeof(info),SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES)){std::string type = info.szTypeName;file_type = type.c_str();}}return file_type;
}QIcon Setting::getFolderIcon()
{QIcon folder_icon = QIcon(":/icon/folder");//将QString转化为std::stringstd::string str = "folder";LPCSTR name = str.c_str();SHFILEINFOA info;if(SHGetFileInfoA(name,FILE_ATTRIBUTE_DIRECTORY,&info,sizeof(info),SHGFI_SYSICONINDEX | SHGFI_ICON | SHGFI_USEFILEATTRIBUTES)){HICON h_icon = info.hIcon;folder_icon = QIcon(QPixmap::fromWinHICON(h_icon)); }return folder_icon;
}QString Setting::getFolderType()
{QString folder_type = QString("folder");std::string str = "folder";LPCSTR name = str.c_str();SHFILEINFOA info;if(SHGetFileInfoA(name,FILE_ATTRIBUTE_DIRECTORY,&info,sizeof(info),SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES)){std::string type = info.szTypeName;folder_type = type.c_str();}return folder_type;
}
两种方式最大的区别是什么呢?参考:filesystems - Qt 4.8 - QFileIconProvider, Getting icon for non-existent file (based on extension) - Stack Overflow 诸如所言,Qt所提供的获取图标的方法是本地已存在的文件,如果要通过扩展名来获取图标则需要自己来虚拟构造本地文件,而在数据量较大的情况下往往会影响性能。这(通过扩展名获取图标、类型)可以通过Windows API来实现,即方法二。
Qt5开始QPixmap::fromWinHICON(HICON)已经消失得无影无踪,且获取图标、后缀的方法仍针对本地已存在的文件,那么,只能另寻出路,此节讲述如何通过扩展名来创建临时文件,并获取对应的图标、类型。
步骤:
通过扩展名extension(.***)构造临时文件QTemporaryFile
通过QFileIconProvider获取对应的图标、类型
删除临时文件
获取图标QIcon FileIcon::fileExtensionIcon(const QString & extension) const
{
QFileIconProvider icon_provider;
QIcon icon;
QTemporaryFile tmp_file(QDir::tempPath() + QDir::separator() +
QCoreApplication::applicationName() + "_XXXXXX" + extension);
tmp_file.setAutoRemove(false);if(tmp_file.open())
{
QString file_name = tmp_file.fileName();
tmp_file.write(QByteArray());
tmp_file.close();icon = icon_provider.icon(QFileInfo(file_name));
tmp_file.remove();
}
else
{
qCritical()<<QString("failed to write temporary file %1") .arg(tmp_file.fileName());
}return icon;
}
获取类型QString FileIcon::fileExtensionType(const QString & extension) const
{
QFileIconProvider icon_provider;
QString type;
QTemporaryFile tmp_file(QDir::tempPath() + QDir::separator() +
QCoreApplication::applicationName() + "_XXXXXX" + extension);
tmp_file.setAutoRemove(false);if(tmp_file.open())
{
QString file_name = tmp_file.fileName();
tmp_file.write(QByteArray());
tmp_file.close();type = icon_provider.type(QFileInfo(file_name));tmp_file.remove();
}
else
{
qCritical()<<QString("failed to write temporary file %1") .arg(tmp_file.fileName());
}return type;
}