如何在QFileSystemModel中显示文件夹的大小
在Qt里面,有一种Model/View框架,Model负责收集信息,View负责显示信息。QFileSystemModel可以读取文件大小,但是默认情况下不能读取文件夹大小。
QFileSystemModel里面有一个size()函数,获取一个index对应的文件的大小。
qint64 QFileSystemModel::size(const QModelIndex &index) const
{
Q_D(const QFileSystemModel);
if (!index.isValid())
return ;
return d->node(index)->size();
}
这里的Q_D指针通过宏定义指向QFileSystemModel的私有类:QFileSystemPrivate。
#define Q_D(Class) Class##Private * const d = d_func()
写一个类MyQFileSystemInfo继承QFileSystemInfo,重写它的size()函数,对具体文件直接返回文件大小,而对文件夹则递归加和计算每个子文件夹的大小以得到它的大小。
可惜,仍然还是不能在view中实时显示文件夹的大小,看来view中使用的size并不是model的size()函数。github的代码链接:https://github.com/1171597779/big_file_inspector。
在QFileSystemModelPrivate里面也有一个size()函数。
QString QFileSystemModelPrivate::size(const QModelIndex &index) const
{
if (!index.isValid())
return QString();
const QFileSystemNode *n = node(index);
if (n->isDir()) {
#ifdef Q_OS_MAC
return QLatin1String("--");
#else
return QLatin1String("");
#endif
// Windows - ""
// OS X - "--"
// Konqueror - "4 KB"
// Nautilus - "9 items" (the number of children)
}
return size(n->size());
}
上面函数的17行调用了重载size()函数:
QString QFileSystemModelPrivate::size(qint64 bytes)
{
// According to the Si standard KB is 1000 bytes, KiB is 1024
// but on windows sizes are calculated by dividing by 1024 so we do what they do.
const qint64 kb = ;
const qint64 mb = * kb;
const qint64 gb = * mb;
const qint64 tb = * gb;
if (bytes >= tb)
return QFileSystemModel::tr("%1 TB").arg(QLocale().toString(qreal(bytes) / tb, 'f', ));
if (bytes >= gb)
return QFileSystemModel::tr("%1 GB").arg(QLocale().toString(qreal(bytes) / gb, 'f', ));
if (bytes >= mb)
return QFileSystemModel::tr("%1 MB").arg(QLocale().toString(qreal(bytes) / mb, 'f', ));
if (bytes >= kb)
return QFileSystemModel::tr("%1 KB").arg(QLocale().toString(bytes / kb));
return QFileSystemModel::tr("%1 bytes").arg(QLocale().toString(bytes));
}
QFileSystemModel的头文件里声明了私有类QFileSystemPrivate,转到这个私有类的头文件里面去,在这个类里定义了另外一个类QFileSystemNode。在这个类的构造函数中,出现一个QExtendedInformation*指针类型的info实参。这个实参在QFileSystemPrivate类里面是一个公有成员,文件或者文件夹的大小就是通过下面的QFileSystemNode的内联函数实现的。
inline qint64 size() const { if (info && !info->isDir()) return info->size(); return ; }
QExtendedinformation类中存在一个QFileInfo类的成员mFileInfo,并通过size()方法来根据具体情况获取文件大小:
qint64 size() const {
qint64 size = -;
if (type() == QExtendedInformation::Dir)
size = ;
if (type() == QExtendedInformation::File)
size = mFileInfo.size();
if (!mFileInfo.exists() && !mFileInfo.isSymLink())
size = -;
return size;
}
而QFileInfo的size()方法如下:
qint64 QFileInfo::size() const
{
Q_D(const QFileInfo);
if (d->isDefaultConstructed)
return ;
if (d->fileEngine == ) {
if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::SizeAttribute))
QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::SizeAttribute);
return d->metaData.size();
}
if (!d->getCachedFlag(QFileInfoPrivate::CachedSize)) {
d->setCachedFlag(QFileInfoPrivate::CachedSize);
d->fileSize = d->fileEngine->size();
}
return d->fileSize;
}
问题来了,要计算一个文件夹的大小,需要很长的时间,不能用内联函数,因此需要重载一下,放弃内联函数的写法。
总结一下,QFileSystemModel里面的size()调用QFileSystemModelPrivate的size(),而这个函数里会使用的QFileSystemNode的内联函数size(),这里面又转到QExtendedinformation里面去,最后是QFileInfo的size()方法。通过重写QFileSystemModel的size()函数并不能实现treeview中文件夹大小的实时显示,需要找出treeview中什么时候调用size()函数。
从QFileSystemModelPrivate类的init()函数可以看到一些端倪:
void QFileSystemModelPrivate::init()
{
Q_Q(QFileSystemModel);
qRegisterMetaType<QVector<QPair<QString,QFileInfo> > >();
#ifndef QT_NO_FILESYSTEMWATCHER
q->connect(&fileInfoGatherer, SIGNAL(newListOfFiles(QString,QStringList)),
q, SLOT(_q_directoryChanged(QString,QStringList)));
q->connect(&fileInfoGatherer, SIGNAL(updates(QString,QVector<QPair<QString,QFileInfo> >)),
q, SLOT(_q_fileSystemChanged(QString,QVector<QPair<QString,QFileInfo> >)));
q->connect(&fileInfoGatherer, SIGNAL(nameResolved(QString,QString)),
q, SLOT(_q_resolvedName(QString,QString)));
q->connect(&fileInfoGatherer, SIGNAL(directoryLoaded(QString)),
q, SIGNAL(directoryLoaded(QString)));
#endif // !QT_NO_FILESYSTEMWATCHER
q->connect(&delayedSortTimer, SIGNAL(timeout()), q, SLOT(_q_performDelayedSort()), Qt::QueuedConnection); roleNames.insertMulti(QFileSystemModel::FileIconRole, QByteArrayLiteral("fileIcon")); // == Qt::decoration
roleNames.insert(QFileSystemModel::FilePathRole, QByteArrayLiteral("filePath"));
roleNames.insert(QFileSystemModel::FileNameRole, QByteArrayLiteral("fileName"));
roleNames.insert(QFileSystemModel::FilePermissions, QByteArrayLiteral("filePermissions"));
}
通过看到QFileSystemModelPrivate里面的方法addNote()才看到一些希望:
QFileSystemModelPrivate::QFileSystemNode* QFileSystemModelPrivate::addNode(QFileSystemNode *parentNode, const QString &fileName, const QFileInfo& info)
{
// In the common case, itemLocation == count() so check there first
QFileSystemModelPrivate::QFileSystemNode *node = new QFileSystemModelPrivate::QFileSystemNode(fileName, parentNode);
#ifndef QT_NO_FILESYSTEMWATCHER
node->populate(info);
#else
Q_UNUSED(info)
#endif
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
//The parentNode is "" so we are listing the drives
if (parentNode->fileName.isEmpty()) {
wchar_t name[MAX_PATH + ];
//GetVolumeInformation requires to add trailing backslash
const QString nodeName = fileName + QLatin1String("\\");
BOOL success = ::GetVolumeInformation((wchar_t *)(nodeName.utf16()),
name, MAX_PATH + , NULL, , NULL, NULL, );
if (success && name[])
node->volumeName = QString::fromWCharArray(name);
}
#endif
Q_ASSERT(!parentNode->children.contains(fileName));
parentNode->children.insert(fileName, node);
return node;
}
上面的node->populate(),这里面就提取了文件信息:
void populate(const QExtendedInformation &fileInfo) {
if (!info)
info = new QExtendedInformation(fileInfo.fileInfo());
(*info) = fileInfo;
}
如何在QFileSystemModel中显示文件夹的大小的更多相关文章
- 【聊技术】在Android中实现自适应文本大小显示
本周的聊技术话题和大家说说如何在Android中实现自适应文本大小显示. 想象一下,在布局中,通常显示文本的区域大小是固定的,但是文本长度并不总是固定的.比如列表中的文章标题.界面下方的按钮文本等等. ...
- iOS中计算磁盘缓存文件夹的大小
SDWebImage框架中在自动做磁盘缓存的过程中,底层实现了计算Cache的大小,框架的方法名称是getSize,但方法不容易被人理解,我就从新写了一下,附带注释 基本思想: 1. 先取出的Cach ...
- 通过命令“du–sk”, “du–Ask” 的区别,谈谈如何在有保护的文件系统中查看文件或文件夹的大小
我们都知道,在Windows中,右键单击一个文件或文件夹,选属性(Properties)可以看到这个文件或文件夹的大小.而这个大小是文件的原始大小,即逻辑大小(logical size).即一个1KB ...
- Linux中如何查看文件夹的大小
直接查看当前文件夹的大小: du –sh 只看文件夹的名字里包含某字符串的子文件夹的大小: du –h –d 1 | grep "BACKEND" 我的linux系统被阉割的比较厉 ...
- 如何在Linux中使用sFTP上传或下载文件与文件夹
如何在Linux中使用sFTP上传或下载文件与文件夹 sFTP(安全文件传输程序)是一种安全的交互式文件传输程序,其工作方式与 FTP(文件传输协议)类似. 然而,sFTP 比 FTP 更安全;它通过 ...
- 如何在Linux中自动删除或清理/tmp文件夹内容?
每个Linux系统都有一个名为的目录/tmp,该目录已挂载了单独的文件系统. 它具有称为tmpfs的特殊文件系统.这是一个虚拟文件系统,操作系统将在系统引导时自动挂载/tmp挂载点. 如果要根据应用程 ...
- [TC]Total Command显示文件夹大小
在Windows下计算文件夹的大小 按下 Alt +Shift +Enter 将会计算当前目录下的文件夹大小. 关于TC相关的知识:TC(Total Commander)文件管理神器
- 如何在Axure中使用FontAwesome字体图标
Font Awesome为您提供可缩放的矢量图标,您可以使用CSS所提供的所有特性对它们进行更改,包括:大小.颜色.阴影或者其它任何支持的效果. FontAwesome应用在web网页开发中非常方便, ...
- 如何在ROS中使用PCL—数据格式(1)
在ROS中点云的数据类型 在ROS中表示点云的数据结构有: sensor_msgs::PointCloud sensor_msgs::PointCloud2 pcl::PointCl ...
随机推荐
- Python数据分析Pandas库方法简介
Pandas 入门 Pandas简介 背景:pandas是一个Python包,提供快速,灵活和富有表现力的数据结构,旨在使“关系”或“标记”数据的使用既简单又直观.它旨在成为在Python中进行实际, ...
- Gym - 100989H
After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...
- tomcat下面web应用发布路径配置 ( 即虚拟目录配置 )
https://blog.csdn.net/AnQ17/article/details/52122236
- UVA1203 Argus
思路 用堆维护每个触发器的下一个事件,每次取出一个事件再把对应触发器的下一个事件加入堆即可 代码 #include <cstdio> #include <algorithm> ...
- centos7安装bbr
centos7安装bbr 安装 sudo wget --no-check-certificate https://github.com/teddysun/across/raw/master/bbr.s ...
- 20190316xlVba_设置行高的改进方案
Public Sub AutoSetRowHeight(ByVal sht As Worksheet, Optional RowsInOnePage As Long) Dim BreakRow As ...
- 做h5动画会用到的一个很好的缓动算法库
http://www.zhangxinxu.com/wordpress/2016/12/how-use-tween-js-animation-easing/
- Docker Image管理学习笔记,ZT
Docker Image管理学习笔记 http://blog.csdn.net/junjun16818/article/details/38423391
- React文档(十三)思考React
在我们的看来,React是使用js创建大型快速网站应用的首要方法.它在Facebook和Instagram的使用已经为我们展现了它自己. React的一个很好的地方就在于当你创建应用的时候它使你思考如 ...
- MongoDB学习笔记——数据库的创建与初始
Part1:MongoDB与SQL的概念对比 图片来源--菜鸟教程 Part2:MongoDB安装地址 直接下载地址:戳这里 备用地址:戳这里 通过备用地址(官网)下载时,要注意下面这个地方 Part ...