Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to check the available free space of a selected drive in Qt?

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 4.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vishnu
    wrote on 17 Dec 2017, 15:03 last edited by
    #1

    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?

    A M 2 Replies Last reply 17 Dec 2017, 15:18
    0
    • V vishnu
      17 Dec 2017, 15:03

      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?

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 17 Dec 2017, 15:18 last edited by
      #2

      @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 has to stay free or it will die.

      R 1 Reply Last reply 18 Dec 2017, 08:46
      2
      • V vishnu
        17 Dec 2017, 15:03

        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?

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 17 Dec 2017, 15:41 last edited by
        #3

        @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:"

        1 Reply Last reply
        3
        • A aha_1980
          17 Dec 2017, 15:18

          @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
          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 18 Dec 2017, 08:46 last edited by raven-worx
          #4

          @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

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          V 1 Reply Last reply 19 Dec 2017, 22:15
          4
          • R raven-worx
            18 Dec 2017, 08:46

            @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

            V Offline
            V Offline
            vishnu
            wrote on 19 Dec 2017, 22:15 last edited by
            #5

            @raven-worx
            you're correct.

            I have changed the constructor. Now it works.

            1 Reply Last reply
            1

            3/5

            17 Dec 2017, 15:41

            • Login

            • Login or register to search.
            3 out of 5
            • First post
              3/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved