QStorageInfo Class
提供有关当前安装的存储和驱动器的信息。更多
头文件: | #include <QStorageInfo> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
- 所有成员(包括继承成员)的列表
- QStorageInfo 属于输入/输出和网络以及隐式共享类。
该类可等价比较。
公共函数
QStorageInfo() | |
QStorageInfo(const QDir &dir) | |
QStorageInfo(const QString &path) | |
QStorageInfo(const 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存储=QStorageInfo::root(); qDebug() << storage.rootPath(); 如果(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 对象的副本。
[noexcept]
QStorageInfo::~QStorageInfo()
销毁QStorageInfo 对象并释放其资源。
int QStorageInfo::blockSize() const
返回该文件系统的最佳传输块大小。
如果QStorageInfo 无法确定大小或QStorageInfo 对象无效,则返回 -1。
qint64 QStorageInfo::bytesAvailable() const
返回当前用户的可用大小(以字节为单位)。如果用户是根用户或系统管理员,则返回可用的总大小。
该大小可以小于或等于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)上,它会返回本地存储设备的 devpath,如/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。例如,如果光盘卷未插入,则返回 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 上的 "绑定 "挂载或 Btrfs 文件系统子卷)。如果能检测到子卷,该函数将返回其名称。子卷名称的格式取决于每种文件系统类型。
如果该卷不是从更大文件系统的子卷挂载的,或者无法检测到子卷,该函数将返回一个空字节数组。
另请参见 device()。
[noexcept]
void QStorageInfo::swap(QStorageInfo &other)
将此卷信息与other 互换。该操作速度非常快,从未出现过故障。
[noexcept]
QStorageInfo &QStorageInfo::operator=(QStorageInfo &&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 对象的结果总是正数。
© 2025 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.