How to determine how much free space on a drive in Qt?(如何确定Qt驱动器上有多少可用空间?)
问题描述
我正在使用 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();如果 (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驱动器上有多少可用空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何确定Qt驱动器上有多少可用空间?


基础教程推荐
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04