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

时间:2012-08-14 06:04:21

标签: qt filesystems qtgui

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

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

这是我的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);
}