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

xiaoxiao2021-02-28  26

简述

在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); } 1234567891011121314151617181920212223242526272829303132

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

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

转载请注明原文地址: https://www.6miu.com/read-1400410.html

最新回复(0)