繁体   English   中英

C ++将文件从Qt传输到外部USB驱动器

[英]C++ Transfer Files From Qt to External USB Drive

我是Qt的新手,我需要帮助将所有文件从本地计算机的特定路径传输到外部USB驱动器。

复制单个文件

您可以使用QFile::copy

QFile::copy(srcPath, dstPath);

注意:此功能不会覆盖文件,因此,如果存在以前的文件,则必须删除它们:

if (QFile::exist(dstPath)) QFile::remove(dstPath);

如果您需要显示一个用户界面来获取源路径和目标路径,则可以使用QFileDialog的方法来实现。 例:

bool copyFiles() {
  const QString srcPath = QFileDialog::getOpenFileName(this, "Source file", "",
    "All files (*.*)");
  if (srcPath.isNull()) return false; // QFileDialog dialogs return null if user canceled

  const QString dstPath = QFileDialog::getSaveFileName(this, "Destination file", "",
    "All files (*.*)"); // it asks the user for overwriting existing files
  if (dstPath.isNull()) return false;

  if (QFile::exist(dstPath))
    if (!QFile::remove(dstPath)) return false; // couldn't delete file
      // probably write-protected or insufficient privileges

  return QFile::copy(srcPath, dstPath);
}

复制目录的全部内容

我将答案扩展到srcPath是目录的情况。 必须手动并递归完成。 这是执行此操作的代码,无需进行错误检查即可简化操作。 您必须负责选择正确的方法(有关一些想法,请QFileInfo::isFile

void recursiveCopy(const QString& srcPath, const QString& dstPath) {
  QDir().mkpath(dstPath); // be sure path exists

  const QDir srcDir(srcPath);
  Q_FOREACH (const auto& dirName, srcDir.entryList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)) {
    recursiveCopy(srcPath + "/" + dirName, dstPath + "/" + dirName);
  }

  Q_FOREACH (const auto& fileName, srcDir.entryList(QStringList(), QDir::Files, QDir::Name)) {
    QFile::copy(srcPath + "/" + fileName, dstPath + "/" + fileName);
  }
}

如果需要询问目录,可以使用QFileDialog::getExistingDirectory

结束语

两种方法都假定srcPath存在。 如果使用QFileDialog方法,则很有可能存在它(很有可能,因为它不是原子操作,并且对话框或复制操作之间的目录或文件可能会被删除或重命名,但这是一个不同的问题)。

我已经解决了QStorageInfo::mountedVolumes()的问题,该问题返回了连接到计算机的设备列表。 但是除了Pendrive或HDD以外,其他所有人都没有名字。 因此(!(storage.name()).isEmpty()))它只会将路径返回到那些设备。

QString location;
QString path1= "/Report/1.txt";
QString locationoffolder="/Report";

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

           qDebug() << "path:" << storage.rootPath();
           //WILL CREATE A FILE IN A BUILD FOLDER
           location = storage.rootPath();
           QString srcPath = "writable.txt";
           //PATH OF THE FOLDER IN PENDRIVE
           QString destPath = location+path1;
           QString folderdir = location+locationoffolder;
           //IF FOLDER IS NOT IN PENDRIVE THEN IT WILL CREATE A FOLDER NAME REPORT
           QDir dir(folderdir);
           if(!dir.exists()){
              dir.mkpath(".");
           }

           qDebug() << "Usbpath:" <<destPath;
           if (QFile::exists(destPath)) QFile::remove(destPath);
           QFile::copy(srcPath,destPath);
           qDebug("copied");

        }
    }
}

由于需要,我必须创建一个文件夹以及USB文件夹,并且为文件指定了一个静态名称。 然后,我只是借助QFile::copy(srcPath, dstPath)将数据从本地计算机的文件复制到了我在USB中创建的文件。 希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM