4

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

任何帮助,将不胜感激!

4

1 回答 1

3

这是我的 Windows 解决方案:

图标提供者.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;
};

图标提供者.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);
}
于 2012-08-14T10:00:58.670 回答