Qt之QFileIconProvider(根据扩展名获取文件图标、类型)
来源:互联网 发布:windows 域控 编辑:程序博客网 时间:2024/05/22 11:02
简述
在Qt之QFileIconProvider一节中已经讲解关于如何获取文件图标与类型。但只仍针对本地已存在的文件,此节,我们主要运用前面分享的内容,讲述如何通过任意后缀或本地不存在的文件来获取相关的图标与类型。
- 简述
- 步骤
- 示例
- 效果
- 源码
- 更多参考
步骤
因为Qt本身并没有提供对于不存在文件获取图标、类型的接口,所以我们只能另寻出路。
步骤:
- 通过扩展名(如:txt、exe、pptx)构建临时文件QTemporaryFile。
- 通过QFileIconProvider获取对应的图标、类型 。
- 删除临时文件。
示例
效果
源码
获取图标、类型
QIcon fileIcon(const QString &extension) const{ QFileIconProvider provider; QIcon icon; QString strTemplateName = QDir::tempPath() + QDir::separator() + QCoreApplication::applicationName() + "_XXXXXX." + extension; QTemporaryFile tmpFile(strTemplateName); tmpFile.setAutoRemove(false); if (tmpFile.open()) { tmpFile.close(); icon = provider.icon(QFileInfo(strFileName)); // tmpFile.remove(); } else { qCritical() << QString("failed to write temporary file %1").arg(tmpFile.fileName()); } return icon;}QString fileType(const QString &extension) const{ QFileIconProvider provider; QString strType; QString strFileName = QDir::tempPath() + QDir::separator() + QCoreApplication::applicationName() + "_XXXXXX." + extension; QTemporaryFile tmpFile(strFileName); tmpFile.setAutoRemove(false); if (tmpFile.open()) { tmpFile.close(); strType = provider.type(QFileInfo(tmpFile.fileName())); // tmpFile.remove(); } else { qCritical() << QString("failed to write temporary file %1").arg(tmpFile.fileName()); } return strType;}
根据扩展名获取对应的文件图标、类型
QListWidget *pListWidget = new QListWidget(this);pListWidget->setIconSize(QSize(48, 48)); // 设置单元项图片大小pListWidget->setResizeMode(QListView::Adjust); // 设置大小模式-可调节pListWidget->setViewMode(QListView::IconMode); // 设置显示模式pListWidget->setMovement(QListView::Static); // 设置单元项不可被拖动pListWidget->setSpacing(10); // 设置单元项间距QStringList strList;strList << QString::fromLocal8Bit("exe") << QString::fromLocal8Bit("zip") << QString::fromLocal8Bit("har") << QString::fromLocal8Bit("hwl") << QString::fromLocal8Bit("accdb") << QString::fromLocal8Bit("xlsx") << QString::fromLocal8Bit("pptx") << QString::fromLocal8Bit("docx") << QString::fromLocal8Bit("jnt") << QString::fromLocal8Bit("txt");for (int i = 0; i < strList.count(); ++i){ // 获取图标、类型 QIcon icon = fileIcon(strList.at(i)); QString strType = fileType(strList.at(i)); // 添加单元项 QListWidgetItem *pItem = new QListWidgetItem(pListWidget); pItem->setIcon(icon); pItem->setText(strType); pListWidget->addItem(pItem);}
更多参考
- Qt之QFileIconProvider
- Qt之QTemporaryFile
- Qt之根据扩展名获取文件图标、类型
0 0
- Qt之QFileIconProvider(根据扩展名获取文件图标、类型)
- Qt之QFileIconProvider(根据扩展名获取文件图标、类型)
- Qt-QFileIconProvider(获取文件图标、类型)
- Qt之QFileIconProvider(获取文件图标、类型)
- Qt之QFileIconProvider续(获取文件图标、类型)
- Qt之QFileIconProvider(获取文件图标、类型)
- Qt之根据扩展名获取文件图标、类型
- Qt之根据扩展名获取文件图标、类型
- QT之 QFileIconProvider 获取文件—超大图标
- C++根据扩展名获取文件图标、类型
- 根据文件名或文件扩展名获取文件的默认图标
- 根据文件名或文件扩展名获取文件的默认图标
- Qt之QFileIconProvider
- 根据扩展名获取文件列表
- Java获取文件的类型(扩展名)
- 获取与文件扩展名关联的图标
- 根据扩展名, 从注册表中取得文件的图标
- Delphi 中 listview 根据文件扩展名 设置系统图标
- Solr6+Suggest(提示)
- Out of memory CheckErrorStatus(Int32 status)
- HDOJ_Easier Done Than Said
- JavaScript Promise 实现(三)
- 骑士
- Qt之QFileIconProvider(根据扩展名获取文件图标、类型)
- 马特-福特无奈为何外界对卡特勒评价糟糕
- jQuery获取Select选中的Text和Value,根据Value值动态添加属性等
- 使用ant打包android生成apk文件
- org.springframework.beans.MethodInvocationException: Property 'cacheManager' threw exception; neste
- hdu5319(模拟)
- C++轻量级界面库DuiLib简介(二)—使用DuiLib
- 802.11N blockACK
- Spring 框架简介