代码之家  ›  专栏  ›  技术社区  ›  dwj

如何确定Qt中驱动器上有多少可用空间?

  •  15
  • dwj  · 技术社区  · 16 年前

    我正在使用qt,想要一种独立于平台的方法来获得可用的磁盘空间。

    我知道在Linux中我可以使用 statfs 在窗户里我可以用 GetDiskFreeSpaceEx() . 我知道Boost有办法, boost::filesystem::space(Path const & p) .

    但我不想要那些。我在qt,我想用qt友好的方式。

    我看着 QDir , QFile , QFileInfo --什么都没有!

    7 回复  |  直到 7 年前
        1
  •  20
  •   Mr.Coffee    10 年前

    我知道这是个很古老的话题,但还是有人觉得它很有用。

    从Qt 5.4开始 QSystemStorageInfo 已经停产了,取而代之的是一个新班级 QStorageInfo 这使得整个任务非常简单,而且是跨平台的。

    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";
    

    代码已从中的示例复制 QT 5.5 docs

        2
  •  11
  •   Violet Giraffe    7 年前

    新的 QStorageInfo 在Qt5.4中引入的类可以做到这一点(以及更多)。它是Qt核心模块的一部分,因此不需要额外的依赖关系。

    #include <QStorageInfo>
    #include <QDebug>
    
    void printRootDriveInfo() {
       QStorageInfo storage = QStorageInfo::root();
    
       qDebug() << storage.rootPath();
       if (storage.isReadOnly())
           qDebug() << "isReadOnly:" << storage.isReadOnly();
    
       qDebug() << "name:" << storage.name();
       qDebug() << "filesystem type:" << storage.fileSystemType();
       qDebug() << "size:" << storage.bytesTotal()/1024/1024 << "MB";
       qDebug() << "free space:" << storage.bytesAvailable()/1024/1024 << "MB";
    }
    
        3
  •  6
  •   dwj    10 年前

    写作时qt中没有任何内容。

    考虑评论或投票 QTBUG-3780 .

        4
  •  5
  •   dwj    11 年前

    当我写下这个问题(在投票后 QTBUG-3780 )我想我会从零开始拯救别人(或我自己)。

    这是Qt 4.8.x的。

    #ifdef WIN32
    /*
     * getDiskFreeSpaceInGB
     *
     * Returns the amount of free drive space for the given drive in GB. The
     * value is rounded to the nearest integer value.
     */
    int getDiskFreeSpaceInGB( LPCWSTR drive )
    {
        ULARGE_INTEGER freeBytesToCaller;
        freeBytesToCaller.QuadPart = 0L;
    
        if( !GetDiskFreeSpaceEx( drive, &freeBytesToCaller, NULL, NULL ) )
        {
            qDebug() << "ERROR: Call to GetDiskFreeSpaceEx() failed.";
        }
    
        int freeSpace_gb = freeBytesToCaller.QuadPart / B_per_GB;
        qDebug() << "Free drive space: " << freeSpace_gb << "GB";
    
        return freeSpace_gb;
    }
    #endif
    

    用途:

    // Check available hard drive space
    #ifdef WIN32
            // The L in front of the string does some WINAPI magic to convert
            // a string literal into a Windows LPCWSTR beast.
            if( getDiskFreeSpaceInGB( L"c:" ) < MinDriveSpace_GB )
            {
                errString = "ERROR: Less than the recommended amount of free space available!";
                isReady = false;
            }
    #else
    #    pragma message( "WARNING: Hard drive space will not be checked at application start-up!" )
    #endif
    
        5
  •  4
  •   Attila    15 年前

    我需要写一个已安装的U盘,我得到了可用的内存大小,代码如下:

    QFile usbMemoryInfo;
    QStringList usbMemoryLines;
    QStringList usbMemoryColumns;
    
    system("df /dev/sdb1 > /tmp/usb_usage.info");
    usbMemoryInfo.setFileName( "/tmp/usb_usage.info" );
    
    usbMemoryInfo.open(QIODevice::ReadOnly);
    
    QTextStream readData(&usbMemoryInfo);
    
    while (!readData.atEnd())
    {
        usbMemoryLines << readData.readLine();
    }
    
    usbMemoryInfo.close();
    
    usbMemoryColumns = usbMemoryLines.at(1).split(QRegExp("\\s+"));
    QString available_bytes = usbMemoryColumns.at(3);
    
        6
  •  4
  •   fox    14 年前

    我知道这个问题现在已经很老了,但是我搜索了stackoverflow,发现没有人能解决这个问题,所以我决定发布。

    有 Q系统存储信息 类 qt流转 它提供了跨平台的方式来获取有关逻辑驱动器的信息。例如: 逻辑驱动程序() 返回可用作其他方法参数的路径列表: 可用磁盘空间() , 总磁盘空间() 以字节为单位获取可用空间和总驱动器空间。

    用法示例:

    QtMobility::QSystemStorageInfo sysStrgInfo;
    QStringList drives = sysStrgInfo.logicalDrives();
    
    foreach (QString drive, drives)
    {
        qDebug() << sysStrgInfo.availableDiskSpace(drive);
        qDebug() << sysStrgInfo.totalDiskSpace(drive);
    }
    

    此示例以字节为单位打印操作系统中所有逻辑驱动器的可用空间和总空间。别忘了添加 qt流转 在qt项目文件中:

    CONFIG += mobility
    MOBILITY += systeminfo
    

    我在一个我正在做的项目中使用了这些方法,它对我很有用。希望它能帮助别人!

        7
  •  2
  •   cupuyc    11 年前

    此代码对我有效:

    #ifdef _WIN32 //win
        #include "windows.h"
    #else //linux
        #include <sys/stat.h>
        #include <sys/statfs.h>
    #endif
    
    
    bool GetFreeTotalSpace(const QString& sDirPath, double& fTotal, double& fFree)
    {
        double fKB = 1024;
    
        #ifdef _WIN32
    
            QString sCurDir = QDir::current().absolutePath();
            QDir::setCurrent(sDirPath);
    
            ULARGE_INTEGER free,total;
    
            bool bRes = ::GetDiskFreeSpaceExA( 0 , &free , &total , NULL );
    
            if ( !bRes )
                return false;
    
            QDir::setCurrent( sCurDir );
    
            fFree = static_cast<__int64>(free.QuadPart)  / fKB;
            fTotal = static_cast<__int64>(total.QuadPart)  / fKB;
    
        #else // Linux
    
            struct stat stst;
            struct statfs stfs;
    
            if ( ::stat(sDirPath.toLocal8Bit(),&stst) == -1 )
                return false;
    
            if ( ::statfs(sDirPath.toLocal8Bit(),&stfs) == -1 )
                return false;
    
            fFree = stfs.f_bavail * ( stst.st_blksize / fKB );
            fTotal = stfs.f_blocks * ( stst.st_blksize / fKB );
    
        #endif // _WIN32
    
        return true;
    }