c++ - 如何在Qt中检查所选驱动器的可用空间?

标签 c++ qt memory qfile qfiledialog

我在 Windows 7 中使用 Qt5.6.2。我的目标是将 CustomDatafile 保存到选定的驱动器(主要是笔驱动器或本地驱动器或网络驱动器)。

因此,在保存文件之前,我想检查所选路径的可用内存和写访问权限。

从此thread我开始了解 QStorageInfo 类。所以我写了下面的函数。

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(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;
   }
}

这是调试输出:

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"

正如你所看到的,QStorageInfo 总是给我 0 大小,驱动器未准备好。无论我选择什么驱动器,结果总是相同的。有人能指出错误吗?我的问题有什么解决办法吗?预先感谢您。

编辑1: 如果我对 QStorage 构造函数进行硬编码

QStorageInfo storage("D:"); Then Everything works fine. If I give the path QStorageInfo storage("D:\Application Cube DMU65mB.cba");

那就不行了。用户可以选择任何驱动器(本地驱动器或可移动驱动器)。选择文件夹后,我从 QDialog 中获取路径(此处未显示代码)。所以我想如果我只能从路径中获取驱动器信息那么我想我的问题就解决了。现在,我怎样才能只获取驱动器名称?我可以通过解析从 QDialog 获得的路径来完成,但有更好的解决方案吗?谢谢

编辑2: 我已将构造函数更改为QStorageInfo storage(path);。然后它适用于本地驱动器,但如果用户选择可移动驱动器(笔式驱动器),则它不起作用。任何人都知道为什么它不适用于 Pen Drive?谢谢。

最佳答案

问题出在 QStorageInfo 类的构造函数的路径上。如果路径包含文件名,那么它将无法工作。作品如下:

   QFileInfo info(fullPath);
   QString path = info.path();
   QStorageInfo storage(path);

注意:完整路径包含文件名,而路径不包含。

接下来的问题就来了 QStorageInfo::isReadOnly()即使您有写访问权限,也会给您 false 。由于 Windows-NFTS 文件系统,这不起作用。 因此,关键是检查是否可以成功创建临时文件。

  if (storage.isValid() && storage.isReady()) {
      //check write permission
      QString filename(path+"dummyTempJob.job"); //temp file, a workaround to check write access because QFileInfo isWritable and storage.isReadOnly() doesn't work for windows NTFS system
      QFile file(filename);
      if(file.open(QIODevice::WriteOnly)){
          file.remove();//delete the temp file
          checkWriteAccess = true;
      }else{
         error = QObject::tr("No write permissions to %1.").arg(path);
         qCritical()<< error;
      }
   }else{
      error = QObject::tr("Drive %1 is not available.").arg(storage.rootPath());
      qCritical() << error << QStringLiteral(" valid = %1, ready = %2").arg(storage.isValid()).arg(storage.isReady());
   }

关于c++ - 如何在Qt中检查所选驱动器的可用空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47835252/

相关文章:

c++ - native Windows C++ 编程?

c++ - 提升测试无法找到自定义打印

c++ - 构建 Castalia : Call of an overloaded function is ambiguous 时出错

python - 如何知道用户何时完成调整窗口大小?

c++ - 如何在QT项目中包含外部库?

c++ - 在 C++ 中以图形方式为图形绘制顶点

c++ - Qt5 加载 .qm 翻译文件

java - 如何在运行缓慢的基于 Java 的 Web 应用程序上强制执行堆转储,以便找出运行缓慢的位置?

c++ - 我们可以使用 'malloced' 释放 'delete' 内存吗?

c++ - 堆损坏 - "Free Heap block 61af0f0 modified at 61af194 after it was freed"C++