gpt4 book ai didi

Qt 4.8 - QFileIconProvider,获取不存在文件的图标(基于扩展名)

转载 作者:行者123 更新时间:2023-12-03 18:42:15 26 4
gpt4 key购买 nike

我目前正在尝试根据文件扩展名获取图标,但似乎 QFileIconProvider 只有在它实际上可以读取现有文件时才会返回一个图标。有什么办法可以根据文件扩展名获得 QIcon 吗?一种替代方法是编写一个具有所需扩展名的临时文件,但这效率非常低,所以我正在寻找解决方法。

任何帮助,将不胜感激!

最佳答案

这是我的 Windows 解决方案:

iconprovider.h:

class IconProvider
{
public:
static IconProvider * instance();
static QIcon fileIcon(const QString &filename);
static QIcon dirIcon();

private:
IconProvider() {}

private:
static IconProvider *self;
QPixmapCache iconCache;
QFileIconProvider iconProvider;
};

iconprovider.cpp:
IconProvider *IconProvider::self = 0;

IconProvider *IconProvider::instance()
{
if(!self)
self = new IconProvider();
return self;
}

QIcon IconProvider::fileIcon(const QString &filename)
{
QFileInfo fileInfo(filename);
QPixmap pixmap;

#ifdef Q_OS_WIN32

if (fileInfo.suffix().isEmpty() || fileInfo.suffix() == "exe" && fileInfo.exists())
{
return instance()->iconProvider.icon(fileInfo);
}

if (!instance()->iconCache.find(fileInfo.suffix(), &pixmap))
{
// Support for nonexistent file type icons, will reimplement it as custom icon provider later
/* We don't use the variable, but by storing it statically, we
* ensure CoInitialize is only called once. */
static HRESULT comInit = CoInitialize(NULL);
Q_UNUSED(comInit);

SHFILEINFO shFileInfo;
unsigned long val = 0;

val = SHGetFileInfo((const wchar_t *)("foo." + fileInfo.suffix()).utf16(), 0, &shFileInfo,
sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES);

// Even if GetFileInfo returns a valid result, hIcon can be empty in some cases
if (val && shFileInfo.hIcon)
{
pixmap = QPixmap::fromWinHICON(shFileInfo.hIcon);
if (!pixmap.isNull())
{
instance()->iconCache.insert(fileInfo.suffix(), pixmap);
}
DestroyIcon(shFileInfo.hIcon);
}
else
{
// TODO: Return default icon if nothing else found
}
}

#else
// Default icon for Linux and Mac OS X for now
return instance()->iconProvider.icon(fileInfo);
#endif

return QIcon(pixmap);
}

QIcon IconProvider::dirIcon()
{
return instance()->iconProvider.icon(QFileIconProvider::Folder);
}

关于Qt 4.8 - QFileIconProvider,获取不存在文件的图标(基于扩展名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11946601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com