QStorageInfo 类

提供有关当前挂载的存储和驱动器的信息。 更多...

头: #include <QStorageInfo>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

公共函数

QStorageInfo ()
QStorageInfo (const QString & path )
QStorageInfo (const QDir & dir )
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= (const QStorageInfo & other )
QStorageInfo & operator= (QStorageInfo && other )

静态公共成员

QList<QStorageInfo> mountedVolumes ()
QStorageInfo root ()
bool operator!= (const QStorageInfo & first , const QStorageInfo & second )
bool operator== (const QStorageInfo & first , const QStorageInfo & second )

详细描述

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.

    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 ()

Constructs an empty QStorageInfo object.

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

另请参阅 setPath (), isReady (),和 isValid ().

[explicit] QStorageInfo:: QStorageInfo (const QString & path )

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 () 方法。

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());
    if (storage.isValid() && storage.isReady()) {
        // ...
    }
					

另请参阅 setPath ().

[explicit] QStorageInfo:: QStorageInfo (const QDir & dir )

Constructs a new QStorageInfo object that gives information about the volume containing the dir 文件夹。

QStorageInfo:: QStorageInfo (const QStorageInfo & other )

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

[noexcept] QStorageInfo:: ~QStorageInfo ()

销毁 QStorageInfo object and frees its resources.

int QStorageInfo:: blockSize () const

Returns the optimal transfer block size for this filesystem.

返回 -1,若 QStorageInfo could not determine the size or if the QStorageInfo 对象是无效的。

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 () 函数。

返回 -1,若 QStorageInfo 对象是无效的。

另请参阅 bytesTotal () 和 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 ().

返回 -1,若 QStorageInfo 对象是无效的。

另请参阅 bytesTotal () 和 bytesAvailable ().

qint64 QStorageInfo:: bytesTotal () const

Returns the total volume size in bytes.

返回 -1,若 QStorageInfo 对象是无效的。

另请参阅 bytesFree () 和 bytesAvailable ().

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).

另请参阅 rootPath () 和 subvolume ().

QString QStorageInfo:: displayName () const

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

QByteArray QStorageInfo:: fileSystemType () const

返回文件系统的类型名称。

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 .

另请参阅 name ().

bool QStorageInfo:: isReadOnly () const

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

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.

注意, fileSystemType (), name (), bytesTotal (), bytesFree (),和 bytesAvailable () will return invalid data until the volume is ready.

另请参阅 isValid ().

bool QStorageInfo:: isRoot () const

返回 true,若此 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.

另请参阅 root ().

bool QStorageInfo:: isValid () const

返回 true 若 QStorageInfo 指定通过 rootPath exists and is mounted correctly.

另请参阅 isReady ().

[static] QList < QStorageInfo > QStorageInfo:: mountedVolumes ()

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

On Windows, this returns the drives visible in the 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.

    foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) {
        if (storage.isValid() && storage.isReady()) {
            if (!storage.isReadOnly()) {
                // ...
            }
        }
    }
					

另请参阅 root ().

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.

另请参阅 fileSystemType ().

void QStorageInfo:: refresh ()

重置 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.

[static] QStorageInfo QStorageInfo:: root ()

返回 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.

另请参阅 isRoot ().

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 '/'.

另请参阅 setPath () 和 device ().

void QStorageInfo:: setPath (const QString & path )

设置此 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.

另请参阅 rootPath ().

QByteArray QStorageInfo:: subvolume () const

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.

另请参阅 device ().

[noexcept] void QStorageInfo:: swap ( QStorageInfo & other )

Swaps this volume info with other 。此函数非常快且从不失败。

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

Makes a copy of the QStorageInfo 对象 other 并将其赋值给此 QStorageInfo 对象。

[noexcept] QStorageInfo &QStorageInfo:: operator= ( QStorageInfo && other )

赋值 other 到此 QStorageInfo 实例。

相关非成员

bool operator!= (const QStorageInfo & first , const QStorageInfo & second )

返回 true 若 first QStorageInfo object refers to a different drive or volume than the second ;否则返回 false。

bool operator== (const QStorageInfo & first , const QStorageInfo & second )

返回 true 若 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.