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::string
str = 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::string
str = 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::string
std::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;
}
文章来源:https://www.toymoban.com/news/detail-683414.html
文章来源地址https://www.toymoban.com/news/detail-683414.html
到了这里,关于Qt 获取文件图标、类型 QFileIconProvider的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!