如何确定Qt驱动器上有多少可用空间? [英] How to determine how much free space on a drive in Qt?

查看:32
本文介绍了如何确定Qt驱动器上有多少可用空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Qt 并且想要一种独立于平台的方式来获取可用的可用磁盘空间.

我知道在 Linux 中我可以使用 statfs,而在 Windows 中我可以使用 GetDiskFreeSpaceEx().我知道 boost 有一种方法,boost::filesystem::space(Path const & p).

但我不想要那些.我在 Qt 中,并希望以 Qt 友好的方式进行.

我看了QDirQFileQFileInfo——什么都没有!

解决方案

我知道这是一个很老的话题,但仍然有人觉得它很有用.

自 QT 5.4 起,QSystemStorageInfo 已停止使用,取而代之的是一个新类 QStorageInfo,它使整个任务变得非常简单并且是跨平台的.

QStorageInfo storage = QStorageInfo::root();qDebug() <<storage.rootPath();如果 (storage.isReadOnly())qDebug() <<是只读的:"<<storage.isReadOnly();qDebug() <<姓名:"<<存储名称();qDebug() <<文件系统类型:"<<storage.fileSystemType();qDebug() <<尺寸:"<<storage.bytesTotal()/1000/1000 <<"MB";qDebug() <<可用大小:"<<storage.bytesAvailable()/1000/1000 <<"MB";

<块引用>

已从QT 5.5 文档

中的示例中复制代码

I'm using Qt and want a platform-independent way of getting the available free disk space.

I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, boost::filesystem::space(Path const & p).

But I don't want those. I'm in Qt and would like to do it in a Qt-friendly way.

I looked at QDir, QFile, QFileInfo -- nothing!

解决方案

I know It's quite old topic but somebody can still find it useful.

Since QT 5.4 the QSystemStorageInfo is discontinued, instead there is a new class QStorageInfo that makes the whole task really simple and it's cross-platform.

QStorageInfo storage = QStorageInfo::root();

qDebug() << storage.rootPath();
if (storage.isReadOnly())
    qDebug() << "isReadOnly:" << storage.isReadOnly();

qDebug() << "name:" << storage.name();
qDebug() << "fileSystemType:" << storage.fileSystemType();
qDebug() << "size:" << storage.bytesTotal()/1000/1000 << "MB";
qDebug() << "availableSize:" << storage.bytesAvailable()/1000/1000 << "MB";

Code has been copied from the example in QT 5.5 docs

这篇关于如何确定Qt驱动器上有多少可用空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆