QStorageInfo Class
The QStorageInfo class provides information about currently mounted storage and drives. More…
| Header | #include <QStorageInfo> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Since | 5.4 |
List of all members, including inherited members
Public Functions
| QStorageInfo() | |
| QStorageInfo(const QString &path) | |
| QStorageInfo(const QDir &dir) | |
| void | swap(QStorageInfo &other) |
| void | setPath(const QString &path) |
| QString | rootPath() const |
| QByteArray | device() const |
| QByteArray | subvolume() const |
| QByteArray | fileSystemType() const |
| QString | name() const |
| QString | displayName() const |
| qint64 | bytesTotal() const |
| qint64 | bytesFree() const |
| qint64 | bytesAvailable() const |
| int | blockSize() const |
| bool | isRoot() const |
| bool | isReadOnly() const |
| bool | isReady() const |
| bool | isValid() const |
| void | refresh() |
Static Public Members
| QList<QStorageInfo> | mountedVolumes() |
| QStorageInfo | root() |
Detailed Description
A QStorageInfo describes the volume that holds a file or directory: where it is mounted, the device and file system behind it, and how much space it has. Create one for a path, or list every volume with mountedVolumes():
const QStorageInfo storage(QDir::homePath());
if (storage.isValid() && storage.isReady() && storage.bytesAvailable() < needed)
qWarning() << "not enough space on" << storage.displayName();
for (const QStorageInfo &volume : QStorageInfo::mountedVolumes())
qDebug() << volume.rootPath() << volume.fileSystemType() << volume.bytesTotal();
The object is a snapshot: the sizes are those of the moment it was created or last refreshed with refresh().
Differences from Qt
Mounted volumes are read from /proc/self/mountinfo, so the class describes volumes on Linux and Android; elsewhere every path is reported as being on the root volume, whose device and file system type are empty. mountedVolumes() leaves out the kernel's own file systems, such as proc, sysfs and the control groups, and of several volumes mounted on one directory only the one on top. name() is the label that udev lists in /dev/disk/by-label. subvolume() is the path of the mounted subvolume on Btrfs and empty for other file systems.
The class is not built for Zephyr, where QT_NO_STORAGEINFO is defined instead. See Nt on Zephyr.
Member Function Documentation
QStorageInfo::QStorageInfo()
Constructs an invalid storage object: isValid() returns false, the strings are empty and the sizes are -1.
See also setPath().
QStorageInfo::QStorageInfo(const QString &path)
Constructs a storage object for the volume that holds path, which may be a file or a directory anywhere on the volume. The object is invalid if path does not exist.
See also setPath().
QStorageInfo::QStorageInfo(const QDir &dir)
This is an overloaded function.
Constructs a storage object for the volume that holds the directory dir.
int QStorageInfo::blockSize() const
Returns the block size of the file system that is best for reading and writing, in bytes, or -1 if it is not known.
This function was introduced in 5.6.
qint64 QStorageInfo::bytesAvailable() const
Returns the number of bytes that the current user can write to the volume, or -1 if the object is invalid or not ready.
See also bytesTotal() and bytesFree().
qint64 QStorageInfo::bytesFree() const
Returns the number of free bytes on the volume, or -1 if the object is invalid or not ready. This counts the space that is reserved for the administrator; a program without privileges can write only bytesAvailable() bytes.
See also bytesTotal() and bytesAvailable().
qint64 QStorageInfo::bytesTotal() const
Returns the size of the volume in bytes, or -1 if the object is invalid or not ready.
See also bytesFree() and bytesAvailable().
QByteArray QStorageInfo::device() const
Returns the device of the volume as the system names it, such as /dev/nvme0n1p2. For a file system without a device, this is what was mounted, such as tmpfs or the address of a network share.
See also rootPath() and subvolume().
QString QStorageInfo::displayName() const
Returns the name() of the volume, or rootPath() if the volume has no name. Use it to present the volume to the user.
QByteArray QStorageInfo::fileSystemType() const
Returns the type of the file system, such as ext4, btrfs or tmpfs.
See also name().
bool QStorageInfo::isReadOnly() const
Returns true if the volume is mounted read-only.
bool QStorageInfo::isReady() const
Returns true if the volume can be used and its sizes could be read. A volume is not ready, for example, when the medium is not in the drive.
See also isValid().
bool QStorageInfo::isRoot() const
Returns true if this is the volume that holds the root directory of the system.
See also root().
bool QStorageInfo::isValid() const
Returns true if the object describes a mounted volume, that is, if the path it was given exists.
See also isReady().
QList<QStorageInfo> QStorageInfo::mountedVolumes()
Returns the volumes that are mounted now, in the order the system lists them. The root volume is among them.
See also root().
QString QStorageInfo::name() const
Returns the name that the user gave the volume, its label, or an empty string if it has none or the label cannot be found.
See also displayName().
void QStorageInfo::refresh()
Reads the sizes, the read-only state and the name of the volume again. The object describes the same root path as before.
See also bytesAvailable().
QStorageInfo QStorageInfo::root()
Returns the volume that holds the root directory of the system.
See also isRoot().
QString QStorageInfo::rootPath() const
Returns the directory on which the volume is mounted, such as / or /home. It is empty for an invalid object.
See also isRoot() and device().
void QStorageInfo::setPath(const QString &path)
Makes this object describe the volume that holds path, and reads its sizes. The object becomes invalid if path does not exist.
See also rootPath() and refresh().
QByteArray QStorageInfo::subvolume() const
Returns the subvolume that is mounted, for a file system that has them, without a leading slash. It is empty when the whole file system is mounted.
This function was introduced in 5.9.
See also device().
void QStorageInfo::swap(QStorageInfo &other)
Exchanges this storage with other.