Qt之根据扩展名获取文件图标、类型

来源:互联网 发布:nginx反向io代理 编辑:程序博客网 时间:2024/05/23 12:56

简述

在C++根据扩展名获取文件图标、类型一节中我们分享了如何根据扩展名来获取对应的文件图标、类型,下面。我们在Qt中使用它。

  • 简述
  • 示例
    • 效果
    • 源码
  • 更多参考

示例

如下,我们根据扩展名来获取对应的文件图标、类型。

效果

这里写图片描述

源码

首先在pro中添加winextras模块:

QT += winextras
  • 1

然后,在源码中包含:#include <QtWin>,之后,方可使用。

std::string strArray[13] = {"folder", ".exe", ".zip", ".har", ".hwl", ".accdb",                                ".xlsx", ".pptx", ".docx", ".txt", ".h", ".cpp", ".pro"};int nCount = sizeof(strArray) / sizeof(std::string);for (int i = 0; i < nCount ; ++i){    // 获取图标、类型    QPixmap pixmap;    std::string type;    int nPos = -1;    nPos = strArray[i].find(".");    if (nPos >= 0)    {        // Qt4:QPixmap::fromWinHICON(icon)        pixmap = QtWin::fromHICON(fileIcon(strArray[i]));        type = fileType(strArray[i]);    }    else    {        pixmap = QtWin::fromHICON(folderIcon());        type = folderType();    }    QIcon icon;    icon.addPixmap(pixmap);    QString strType = QString::fromLocal8Bit(type.c_str());    // 添加单元项    QListWidgetItem *pItem = new QListWidgetItem(pListWidget);    pItem->setIcon(icon);    pItem->setText(strType);    pListWidget->addItem(pItem);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

在Qt4中,可以通过QPixmap::fromWinHICON(HICON)来转换,但是,到了Qt5以后此接口已经被遗弃了,所以这里使用QtWin::fromHICON(HICON)。

原文链接:http://blog.csdn.net/liang19890820/article/details/51822561

阅读全文
0 0