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

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

问题描述



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

但我不想那些。我在Qt中,并希望以Qt友好的方式。



我看了 QDir QFile QFileInfo - 无!

解决方案

我知道这是相当老的话题,但有人仍然可以找到它的用处。

由于QT 5.4中断了 QSystemStorageInfo ,所以有一个新的类 QStorageInfo 这使得整个任务非常简单,而且是跨平台的。

  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;




代码已经从 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天全站免登陆