How to check the available free space of a selected drive in Qt?



  • I am using Qt5.6.2 in Windows 7. My Goal is to save a CustomDatafile to a selected drive (mostly a Pen drive or local drive or network drive).

    So before saving the file I would like to check the available memory and write access of the selected path.

    From this thread I came to know about QStorageInfo class. So I wrote the below function.

    bool ImportExport::checkWriteAccessAndEnoughDriveSpace(const QString &path,QString &error)
    {
       error = QString();
       int fileSizeMB = 100;//for testing purpose I am comparing with 100 MB. correct solution is to compare with size of the file.
       qDebug() << "path to QStorage: " <<path;
       QStorageInfo storage = QStorageInfo::QStorageInfo(path);
       //QStorageInfo storage = QStorageInfo::root();
       qDebug() << "export root path: " <<storage.rootPath();
       qDebug() << "volume name:" << storage.name();
       qDebug() << "fileSystemType:" << storage.fileSystemType();
       qDebug() << "size:" << storage.bytesTotal()/1000/1000 << "MB";
       qDebug() << "availableSize:" << storage.bytesAvailable()/1000/1000 << "MB";
    
       if (storage.isValid() && storage.isReady()) {
          if (!storage.isReadOnly()) {
             // check enough memory
             int MBavailable = storage.bytesAvailable()/1000/1000;
             if(MBavailable > fileSizeMB){
                return true;
             }else{
                error = tr("Not enough disk space, available disk space is only : ") + QString::number(MBavailable);
                return false;
             }
          }else{
             error = tr("No permission to write to current folder ");
             qDebug() << error; // how to set this message as toolTip on the fileDialog
             return false;
          }
       }else{
          error = tr("Selected drive validity: ")+ QString::number(storage.isValid()) +tr("or storage availability: ") +QString::number(storage.isReady());
          qDebug() << error; // how to set this message as toolTip
          return false;
       }
    }
    

    This is the debug output:

    path to QStorage: "D:/Aluminium Demo DMU105mB.cba"
    export root path: ""
    volume name: ""
    fileSystemType: ""
    size: 0 MB
    availableSize: 0 MB
    "Selected drive validity: 0or storage availability: 0"
    As you can see the QStorageInfo is always giving me 0 size, drive not ready. No matter what ever the drive i select the result is always the same. Can anybody point out the error? any solution for my problem? Thank you in advance.

    Note: isValid() is always false. I am not able to understand why is it false even though the drive is present and i have the write access as well?


  • Qt Champions 2017

    @vishnu said in How to check the available free space of a selected drive in Qt?:

    I am using Qt5.6.2 in Windows 7. My Goal is to save a CustomDatafile to a selected drive (mostly a Pen drive or local drive or network drive).

    So before saving the file I would like to check the available memory and write access of the selected path.

    From this thread I came to know about QStorageInfo class. So I wrote the below function.

    bool ImportExport::checkWriteAccessAndEnoughDriveSpace(const QString &path,QString &error)
    {
       error = QString();
       int fileSizeMB = 100;//for testing purpose I am comparing with 100 MB. correct solution is to compare with size of the file.
       qDebug() << "path to QStorage: " <<path;
       QStorageInfo storage = QStorageInfo::QStorageInfo(path);
       //QStorageInfo storage = QStorageInfo::root();
       qDebug() << "export root path: " <<storage.rootPath();
       qDebug() << "volume name:" << storage.name();
       qDebug() << "fileSystemType:" << storage.fileSystemType();
       qDebug() << "size:" << storage.bytesTotal()/1000/1000 << "MB";
       qDebug() << "availableSize:" << storage.bytesAvailable()/1000/1000 << "MB";
    
       if (storage.isValid() && storage.isReady()) {
          if (!storage.isReadOnly()) {
             // check enough memory
             int MBavailable = storage.bytesAvailable()/1000/1000;
             if(MBavailable > fileSizeMB){
                return true;
             }else{
                error = tr("Not enough disk space, available disk space is only : ") + QString::number(MBavailable);
                return false;
             }
          }else{
             error = tr("No permission to write to current folder ");
             qDebug() << error; // how to set this message as toolTip on the fileDialog
             return false;
          }
       }else{
          error = tr("Selected drive validity: ")+ QString::number(storage.isValid()) +tr("or storage availability: ") +QString::number(storage.isReady());
          qDebug() << error; // how to set this message as toolTip
          return false;
       }
    }
    

    I have two ideas:

    1. change the constructor to QStorageInfo storage(path);
    2. try a filename without spaces

  • Qt Champions 2017

    @vishnu
    Hi
    as @aha_1980 talks about.
    What are you giving it for path ?
    I tried with my ramdrive
    alt text

    and it seemed to work fine with "e:"


  • Moderators

    @aha_1980 said in How to check the available free space of a selected drive in Qt?:

    QStorageInfo storage = QStorageInfo::QStorageInfo(path);

    i guess this is your issue. I am wondering that this even compiles?!?! o.O



  • @raven-worx
    you're correct.

    I have changed the constructor. Now it works.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.