Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
QStorageInfo Class Reference

\inmodule QtCore More...

#include <qstorageinfo.h>

+ Collaboration diagram for QStorageInfo:

Public Member Functions

 QStorageInfo ()
 Constructs an empty QStorageInfo object.
 
 QStorageInfo (const QString &path)
 Constructs a new QStorageInfo object that gives information about the volume mounted at path.
 
 QStorageInfo (const QDir &dir)
 Constructs a new QStorageInfo object that gives information about the volume containing the dir folder.
 
 QStorageInfo (const QStorageInfo &other)
 Constructs a new QStorageInfo object that is a copy of the other QStorageInfo object.
 
 ~QStorageInfo ()
 Destroys the QStorageInfo object and frees its resources.
 
QStorageInfooperator= (const QStorageInfo &other)
 Makes a copy of the QStorageInfo object other and assigns it to this QStorageInfo object.
 
void swap (QStorageInfo &other) noexcept
 Swaps this volume info with other.
 
void setPath (const QString &path)
 Sets this QStorageInfo object to the filesystem mounted where path is located.
 
QString rootPath () const
 Returns the mount point of the filesystem this QStorageInfo object represents.
 
QByteArray device () const
 Returns the device for this volume.
 
QByteArray subvolume () const
 
QByteArray fileSystemType () const
 Returns the type name of the filesystem.
 
QString name () const
 Returns the human-readable name of a filesystem, usually called label.
 
QString displayName () const
 Returns the volume's name, if available, or the root path if not.
 
qint64 bytesTotal () const
 Returns the total volume size in bytes.
 
qint64 bytesFree () const
 Returns the number of free bytes in a volume.
 
qint64 bytesAvailable () const
 Returns the size (in bytes) available for the current user.
 
int blockSize () const
 
bool isRoot () const
 Returns true if this QStorageInfo represents the system root volume; false otherwise.
 
bool isReadOnly () const
 Returns true if the current filesystem is protected from writing; false otherwise.
 
bool isReady () const
 Returns true if the current filesystem is ready to work; false otherwise.
 
bool isValid () const
 Returns true if the QStorageInfo specified by rootPath exists and is mounted correctly.
 
void refresh ()
 Resets QStorageInfo's internal cache.
 

Static Public Member Functions

static QList< QStorageInfomountedVolumes ()
 Returns the list of QStorageInfo objects that corresponds to the list of currently mounted filesystems.
 
static QStorageInfo root ()
 Returns a QStorageInfo object that represents the system root volume.
 

Friends

class QStorageInfoPrivate
 
bool operator== (const QStorageInfo &first, const QStorageInfo &second)
 Returns true if the first QStorageInfo object refers to the same drive or volume as the second; otherwise it returns false.
 
bool operator!= (const QStorageInfo &first, const QStorageInfo &second)
 Returns true if the first QStorageInfo object refers to a different drive or volume than the second; otherwise returns false.
 
Q_CORE_EXPORT QDebug operator<< (QDebug, const QStorageInfo &)
 

Detailed Description

\inmodule QtCore

Since
5.4

Provides information about currently mounted storage and drives.

Allows retrieving information about the volume's space, its mount point, label, and filesystem name.

You can create an instance of QStorageInfo by passing the path to the volume's mount point as a constructor parameter, or you can set it using the setPath() method. The static mountedVolumes() method can be used to get the list of all mounted filesystems.

QStorageInfo always caches the retrieved information, but you can call refresh() to invalidate the cache.

The following example retrieves the most common information about the root volume of the system, and prints information about it.

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";
\inmodule QtCore
QString name() const
Returns the human-readable name of a filesystem, usually called label.
qint64 bytesTotal() const
Returns the total volume size in bytes.
qint64 bytesAvailable() const
Returns the size (in bytes) available for the current user.
static QStorageInfo root()
Returns a QStorageInfo object that represents the system root volume.
QString rootPath() const
Returns the mount point of the filesystem this QStorageInfo object represents.
QByteArray fileSystemType() const
Returns the type name of the filesystem.
bool isReadOnly() const
Returns true if the current filesystem is protected from writing; false otherwise.
#define qDebug
[1]
Definition qlogging.h:160
QStorageInfo storage
[1]

Definition at line 19 of file qstorageinfo.h.

Constructor & Destructor Documentation

◆ QStorageInfo() [1/4]

QStorageInfo::QStorageInfo ( )

Constructs an empty QStorageInfo object.

Objects created with the default constructor will be invalid and therefore not ready for use.

See also
setPath(), isReady(), isValid()

Definition at line 48 of file qstorageinfo.cpp.

◆ QStorageInfo() [2/4]

QStorageInfo::QStorageInfo ( const QString path)
explicit

Constructs a new QStorageInfo object that gives information about the volume mounted at path.

If you pass a directory or file, the QStorageInfo object will refer to the volume where this directory or file is located. You can check if the created object is correct using the isValid() method.

The following example shows how to get the volume on which the application is located. It is recommended to always check that the volume is ready and valid.

QStorageInfo storage(qApp->applicationDirPath());
// ...
}
bool isValid() const
Returns true if the QStorageInfo specified by rootPath exists and is mounted correctly.
bool isReady() const
Returns true if the current filesystem is ready to work; false otherwise.
#define qApp
See also
setPath()

Definition at line 68 of file qstorageinfo.cpp.

References setPath().

+ Here is the call graph for this function:

◆ QStorageInfo() [3/4]

QStorageInfo::QStorageInfo ( const QDir dir)
explicit

Constructs a new QStorageInfo object that gives information about the volume containing the dir folder.

Definition at line 78 of file qstorageinfo.cpp.

References dir, and setPath().

+ Here is the call graph for this function:

◆ QStorageInfo() [4/4]

QStorageInfo::QStorageInfo ( const QStorageInfo other)

Constructs a new QStorageInfo object that is a copy of the other QStorageInfo object.

Definition at line 87 of file qstorageinfo.cpp.

◆ ~QStorageInfo()

QStorageInfo::~QStorageInfo ( )

Destroys the QStorageInfo object and frees its resources.

Definition at line 95 of file qstorageinfo.cpp.

Member Function Documentation

◆ blockSize()

int QStorageInfo::blockSize ( ) const
Since
5.6 Returns the optimal transfer block size for this filesystem.

Returns -1 if QStorageInfo could not determine the size or if the QStorageInfo object is not valid.

Definition at line 206 of file qstorageinfo.cpp.

◆ bytesAvailable()

qint64 QStorageInfo::bytesAvailable ( ) const

Returns the size (in bytes) available for the current user.

It returns the total size available if the user is the root user or a system administrator.

This size can be less than or equal to the free size returned by bytesFree() function.

Returns -1 if QStorageInfo object is not valid.

See also
bytesTotal(), bytesFree()

Definition at line 168 of file qstorageinfo.cpp.

◆ bytesFree()

qint64 QStorageInfo::bytesFree ( ) const

Returns the number of free bytes in a volume.

Note that if there are quotas on the filesystem, this value can be larger than the value returned by bytesAvailable().

Returns -1 if QStorageInfo object is not valid.

See also
bytesTotal(), bytesAvailable()

Definition at line 182 of file qstorageinfo.cpp.

◆ bytesTotal()

qint64 QStorageInfo::bytesTotal ( ) const

Returns the total volume size in bytes.

Returns -1 if QStorageInfo object is not valid.

See also
bytesFree(), bytesAvailable()

Definition at line 194 of file qstorageinfo.cpp.

◆ device()

QByteArray QStorageInfo::device ( ) const

Returns the device for this volume.

For example, on Unix filesystems (including \macos), this returns the devpath like /dev/sda0 for local storages. On Windows, it returns the UNC path starting with \\?\ for local storages (in other words, the volume GUID).

See also
rootPath(), subvolume()

Definition at line 234 of file qstorageinfo.cpp.

◆ displayName()

QString QStorageInfo::displayName ( ) const

Returns the volume's name, if available, or the root path if not.

Definition at line 280 of file qstorageinfo.cpp.

◆ fileSystemType()

QByteArray QStorageInfo::fileSystemType ( ) const

Returns the type name of the filesystem.

This is a platform-dependent function, and filesystem names can vary between different operating systems. For example, on Windows filesystems they can be named NTFS, and on Linux they can be named ntfs-3g or fuseblk.

See also
name()

Definition at line 220 of file qstorageinfo.cpp.

◆ isReadOnly()

bool QStorageInfo::isReadOnly ( ) const

Returns true if the current filesystem is protected from writing; false otherwise.

Definition at line 303 of file qstorageinfo.cpp.

Referenced by foreach().

+ Here is the caller graph for this function:

◆ isReady()

bool QStorageInfo::isReady ( ) const

Returns true if the current filesystem is ready to work; false otherwise.

For example, false is returned if the CD volume is not inserted.

Note that fileSystemType(), name(), bytesTotal(), bytesFree(), and bytesAvailable() will return invalid data until the volume is ready.

See also
isValid()

Definition at line 317 of file qstorageinfo.cpp.

Referenced by foreach().

+ Here is the caller graph for this function:

◆ isRoot()

bool QStorageInfo::isRoot ( ) const
inline

Returns true if this QStorageInfo represents the system root volume; false otherwise.

On Unix filesystems, the root volume is a volume mounted on /. On Windows, the root volume is the volume where the OS is installed.

See also
root()

Definition at line 76 of file qstorageinfo.h.

References root().

+ Here is the call graph for this function:

◆ isValid()

bool QStorageInfo::isValid ( ) const

Returns true if the QStorageInfo specified by rootPath exists and is mounted correctly.

See also
isReady()

Definition at line 328 of file qstorageinfo.cpp.

Referenced by foreach().

+ Here is the caller graph for this function:

◆ mountedVolumes()

QList< QStorageInfo > QStorageInfo::mountedVolumes ( )
static

Returns the list of QStorageInfo objects that corresponds to the list of currently mounted filesystems.

On Windows, this returns the drives visible in the \gui{My Computer} folder. On Unix operating systems, it returns the list of all mounted filesystems (except for pseudo filesystems).

Returns all currently mounted filesystems by default.

The example shows how to retrieve all available filesystems, skipping read-only ones.

if (!storage.isReadOnly()) {
// ...
}
}
}
static QList< QStorageInfo > mountedVolumes()
Returns the list of QStorageInfo objects that corresponds to the list of currently mounted filesystem...
See also
root()

Definition at line 363 of file qstorageinfo.cpp.

◆ name()

QString QStorageInfo::name ( ) const

Returns the human-readable name of a filesystem, usually called label.

Not all filesystems support this feature. In this case, the value returned by this method could be empty. An empty string is returned if the file system does not support labels, or if no label is set.

On Linux, retrieving the volume's label requires udev to be present in the system.

See also
fileSystemType()

Definition at line 272 of file qstorageinfo.cpp.

◆ operator=()

QStorageInfo & QStorageInfo::operator= ( const QStorageInfo other)

Makes a copy of the QStorageInfo object other and assigns it to this QStorageInfo object.

Assigns other to this QStorageInfo instance.

Definition at line 102 of file qstorageinfo.cpp.

References other().

+ Here is the call graph for this function:

◆ refresh()

void QStorageInfo::refresh ( )

Resets QStorageInfo's internal cache.

QStorageInfo caches information about storage to speed up performance. QStorageInfo retrieves information during object construction and/or when calling the setPath() method. You have to manually reset the cache by calling this function to update storage information.

Definition at line 341 of file qstorageinfo.cpp.

References QExplicitlySharedDataPointer< T >::detach().

+ Here is the call graph for this function:

◆ root()

QStorageInfo QStorageInfo::root ( )
static

Returns a QStorageInfo object that represents the system root volume.

On Unix systems this call returns the root ('/') volume; in Windows the volume where the operating system is installed.

See also
isRoot()

Definition at line 378 of file qstorageinfo.cpp.

Referenced by isRoot().

+ Here is the caller graph for this function:

◆ rootPath()

QString QStorageInfo::rootPath ( ) const

Returns the mount point of the filesystem this QStorageInfo object represents.

On Windows, it returns the volume letter in case the volume is not mounted to a directory.

Note that the value returned by rootPath() is the real mount point of a volume, and may not be equal to the value passed to the constructor or setPath() method. For example, if you have only the root volume in the system, and pass '/directory' to setPath(), then this method will return '/'.

See also
setPath(), device()

Definition at line 152 of file qstorageinfo.cpp.

Referenced by freeDesktopTrashLocation().

+ Here is the caller graph for this function:

◆ setPath()

void QStorageInfo::setPath ( const QString path)

Sets this QStorageInfo object to the filesystem mounted where path is located.

path can either be a root path of the filesystem, a directory, or a file within that filesystem.

See also
rootPath()

Definition at line 129 of file qstorageinfo.cpp.

References QExplicitlySharedDataPointer< T >::detach().

Referenced by QStorageInfo(), and QStorageInfo().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ subvolume()

QByteArray QStorageInfo::subvolume ( ) const
Since
5.9 Returns the subvolume name for this volume.

Some filesystem types allow multiple subvolumes inside one device, which may be mounted in different paths (e.g. 'bind' mounts on Unix, or Btrfs filesystem subvolumes). If the subvolume could be detected, its name is returned by this function. The format of the subvolume name is specific to each filesystem type.

If this volume was not mounted from a subvolume of a larger filesystem or if the subvolume could not be detected, this function returns an empty byte array.

See also
device()

Definition at line 255 of file qstorageinfo.cpp.

◆ swap()

void QStorageInfo::swap ( QStorageInfo other)
inlinenoexcept

Swaps this volume info with other.

This function is very fast and never fails.

Definition at line 31 of file qstorageinfo.h.

References d, and other().

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ operator!=

bool QStorageInfo::operator!= ( const QStorageInfo first,
const QStorageInfo second 
)
friend

Returns true if the first QStorageInfo object refers to a different drive or volume than the second; otherwise returns false.

Definition at line 67 of file qstorageinfo.h.

◆ operator<<

Q_CORE_EXPORT QDebug operator<< ( QDebug  ,
const QStorageInfo  
)
friend

Definition at line 401 of file qstorageinfo.cpp.

◆ operator==

bool QStorageInfo::operator== ( const QStorageInfo first,
const QStorageInfo second 
)
friend

Returns true if the first QStorageInfo object refers to the same drive or volume as the second; otherwise it returns false.

Note that the result of comparing two invalid QStorageInfo objects is always positive.

Definition at line 60 of file qstorageinfo.h.

◆ QStorageInfoPrivate

friend class QStorageInfoPrivate
friend

Definition at line 59 of file qstorageinfo.h.


The documentation for this class was generated from the following files: