QStorageInfo Class
提供有关当前已挂载的存储设备和驱动器的信息。更多...
| 标题: | #include <QStorageInfo> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
- 所有成员的列表,包括继承的成员
- QStorageInfo 属于“输入/输出与网络”和“隐式共享类”类别。
QStorageInfo 比较
| 类别 | 可比较类型 |
|---|---|
| 相等性 | QStorageInfo |
公共函数
| QStorageInfo() | |
| QStorageInfo(const QDir &dir) | |
| QStorageInfo(const QString &path) | |
| QStorageInfo(const QStorageInfo &other) | |
(since 6.10) | QStorageInfo(QStorageInfo &&other) |
| ~QStorageInfo() | |
| int | blockSize() const |
| qint64 | bytesAvailable() const |
| qint64 | bytesFree() const |
| qint64 | bytesTotal() const |
| QByteArray | device() const |
| QString | displayName() const |
| QByteArray | fileSystemType() const |
| bool | isReadOnly() const |
| bool | isReady() const |
| bool | isRoot() const |
| bool | isValid() const |
| QString | name() const |
| void | refresh() |
| QString | rootPath() const |
| void | setPath(const QString &path) |
| QByteArray | subvolume() const |
| void | swap(QStorageInfo &other) |
| QStorageInfo & | operator=(QStorageInfo &&other) |
| QStorageInfo & | operator=(const QStorageInfo &other) |
静态公共成员
| QList<QStorageInfo> | mountedVolumes() |
| QStorageInfo | root() |
相关的非成员
| bool | operator!=(const QStorageInfo &lhs, const QStorageInfo &rhs) |
| bool | operator==(const QStorageInfo &lhs, const QStorageInfo &rhs) |
详细说明
允许检索有关卷的空间、挂载点、标签和文件系统名称的信息。
您可以通过将卷的挂载点路径作为构造函数参数传递来创建 QStorageInfo 的实例,也可以使用setPath() 方法进行设置。静态方法mountedVolumes() 可用于获取所有已挂载文件系统的列表。
QStorageInfo 会始终缓存检索到的信息,但您可以调用refresh() 来清空缓存。
以下示例检索系统根卷的最常用信息,并打印相关信息。
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";成员函数文档
QStorageInfo::QStorageInfo()
构建一个空的 QStorageInfo 对象。
使用默认构造函数创建的对象将无效,因此无法使用。
另请参阅 setPath()、isReady() 和isValid()。
[explicit] QStorageInfo::QStorageInfo(const QDir &dir)
创建一个新的 QStorageInfo 对象,该对象提供有关包含dir 文件夹的卷的信息。
[explicit] QStorageInfo::QStorageInfo(const QString &path)
创建一个新的 QStorageInfo 对象,该对象提供关于挂载在path 上的卷的信息。
若传入的是目录或文件,该 QStorageInfo 对象将指向包含该目录或文件的卷。您可以使用isValid() 方法检查创建的对象是否正确。
以下示例演示了如何获取应用程序所在的卷。建议始终检查该卷是否已就绪且有效。
QStorageInfo storage(qApp->applicationDirPath());
if (storage.isValid() && storage.isReady()) {
// ...
}另请参阅 setPath()。
QStorageInfo::QStorageInfo(const QStorageInfo &other)
创建一个新的 QStorageInfo 对象,该对象是other 中的 QStorageInfo 对象的副本。
[constexpr noexcept default, since 6.10] QStorageInfo::QStorageInfo(QStorageInfo &&other)
从 `other` 通过 move 操作构造一个新的 `QStorageInfo`。
被移动的对象other 将处于部分初始化的状态,在此状态下,唯一有效的操作是销毁和赋予新值。
该函数于 Qt 6.10 中引入。
[noexcept] QStorageInfo::~QStorageInfo()
销毁QStorageInfo 对象并释放其资源。
int QStorageInfo::blockSize() const
返回此文件系统的最佳传输块大小。
如果QStorageInfo 无法确定大小,或者QStorageInfo 对象无效,则返回-1。
qint64 QStorageInfo::bytesAvailable() const
返回当前用户可用的空间大小(以字节为单位)。如果用户是root用户或系统管理员,则返回总可用空间大小。
该大小可能小于或等于bytesFree()函数返回的可用空间大小。
如果QStorageInfo 对象无效,则返回 -1。
另请参阅 bytesTotal() 和bytesFree()。
qint64 QStorageInfo::bytesFree() const
返回卷中可用字节的数量。请注意,如果文件系统上设置了配额,该值可能会大于bytesAvailable() 函数返回的值。
如果QStorageInfo 对象无效,则返回 -1。
另请参阅 bytesTotal() 和bytesAvailable()。
qint64 QStorageInfo::bytesTotal() const
返回总卷大小(以字节为单位)。
如果 `QStorageInfo ` 对象无效,则返回 -1。
另请参阅 bytesFree() 和bytesAvailable()。
QByteArray QStorageInfo::device() const
返回该卷对应的设备。
例如,在 Unix 文件系统(包括 macOS)上,对于本地存储,该函数会返回类似/dev/sda0 的设备路径。在 Windows 上,对于本地存储,该函数会返回以\\\\?\\ 开头的 UNC 路径(换言之,即卷 GUID)。
QString QStorageInfo::displayName() const
返回卷的名称(如有),否则返回根路径。
QByteArray QStorageInfo::fileSystemType() const
返回文件系统的类型名称。
这是一个与平台相关的函数,不同操作系统中的文件系统名称可能有所不同。例如,在 Windows 文件系统中,它们可能命名为NTFS ,而在 Linux 中,则可能命名为ntfs-3g 或fuseblk 。
另请参阅 name()。
bool QStorageInfo::isReadOnly() const
如果当前文件系统被设置为只读,则返回 true;否则返回 false。
bool QStorageInfo::isReady() const
如果当前文件系统已准备就绪,则返回 true;否则返回 false。例如,如果未插入 CD 光盘,则返回 false。
请注意,在卷准备就绪之前,fileSystemType()、name()、bytesTotal()、bytesFree() 和bytesAvailable() 将返回无效数据。
另请参阅 isValid()。
bool QStorageInfo::isRoot() const
如果该QStorageInfo 表示系统根卷,则返回true;否则返回false。
在 Unix 文件系统中,根卷是挂载在/ 上的卷。在 Windows 中,根卷是安装了操作系统的卷。
另请参阅 root()。
bool QStorageInfo::isValid() const
如果由 `rootPath ` 指定的 `QStorageInfo ` 存在且已正确挂载,则返回 `true`。
另请参阅 isReady()。
[static] QList<QStorageInfo> QStorageInfo::mountedVolumes()
返回与当前已挂载文件系统列表对应的QStorageInfo 对象列表。
在 Windows 系统上,此函数返回“我的电脑”文件夹中可见的驱动器。在 Unix 操作系统上,它返回所有已挂载文件系统的列表(伪文件系统除外)。
默认情况下,该函数返回所有当前已挂载的文件系统。
该示例演示了如何获取所有可用的文件系统,同时跳过只读文件系统。
foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) {
if (storage.isValid() && storage.isReady()) {
if (!storage.isReadOnly()) {
// ...
}
}
}另请参阅 root()。
QString QStorageInfo::name() const
返回文件系统的可读名称,通常称为label 。
并非所有文件系统都支持此功能。在这种情况下,该方法返回的值可能是空的。如果文件系统不支持标签,或者未设置标签,则返回空字符串。
在 Linux 系统中,要获取卷的标签,系统中必须存在 `udev `。
另请参阅 fileSystemType()。
void QStorageInfo::refresh()
重置QStorageInfo 的内部缓存。
QStorageInfo 该缓存存储了有关存储的信息,以提升性能。QStorageInfo 会在对象构造期间和/或调用setPath()方法时检索这些信息。您必须通过调用此函数手动重置缓存,以更新存储信息。
[static] QStorageInfo QStorageInfo::root()
返回一个表示系统根卷的QStorageInfo 对象。
在 Unix 系统上,此调用返回根('/')卷;在 Windows 系统上,则返回安装操作系统的卷。
另请参阅 isRoot()。
QString QStorageInfo::rootPath() const
返回该QStorageInfo 对象所表示的文件系统的挂载点。
在 Windows 系统上,如果卷未挂载到目录,则返回卷标。
请注意,rootPath() 返回的值是卷的实际挂载点,可能与传递给构造函数或setPath() 方法的值不一致。例如,如果系统中只有根卷,且向setPath() 传递了 '/directory',则该方法将返回 '/'。
void QStorageInfo::setPath(const QString &path)
将此QStorageInfo 对象设置为包含path 的已挂载文件系统。
path 可以是文件系统的根路径、该文件系统中的某个目录,或该文件系统中的某个文件。
另请参阅 rootPath()。
QByteArray QStorageInfo::subvolume() const
返回该卷的子卷名称。
某些文件系统类型允许在一个设备中包含多个子卷,这些子卷可能挂载在不同的路径上(例如 Unix 中的“bind”挂载,或 Btrfs 文件系统的子卷)。如果能够检测到子卷,则该函数将返回其名称。 子卷名称的格式因文件系统类型而异。
如果该卷并非从一个更大文件系统的子卷挂载而来,或者无法检测到子卷,则该函数返回一个空字节数组。
另请参阅 device()。
[noexcept] void QStorageInfo::swap(QStorageInfo &other)
将此卷信息与other 进行交换。此操作速度极快,且绝不会失败。
[noexcept] QStorageInfo &QStorageInfo::operator=(QStorageInfo &&other)
将 `other ` 通过移动赋值操作赋值给此 `QStorageInfo ` 实例。
被移动的对象other 将被置于一种有效但未指定的状态中。
QStorageInfo &QStorageInfo::operator=(const QStorageInfo &other)
复制QStorageInfo 对象other ,并将副本赋值给此QStorageInfo 对象。
相关的非成员
[noexcept] bool operator!=(const QStorageInfo &lhs, const QStorageInfo &rhs)
如果QStorageInfo 对象lhs 所指的驱动器或卷与QStorageInfo 对象rhs 所指的不同,则返回true ;否则返回false 。
[noexcept] bool operator==(const QStorageInfo &lhs, const QStorageInfo &rhs)
如果QStorageInfo 对象lhs 所引用的驱动器或卷与QStorageInfo 对象rhs 所引用的驱动器或卷相同,则返回true ;否则返回false 。
请注意,比较两个无效的QStorageInfo 对象的结果总是正数。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.