QFile::copy说“can't open source file for input”?

时间:2022-06-02 20:52:48

I'm going to copy one file using QFile::copy function but this function always returns false and errorString says :

我将复制一个文件使用QFile::copy函数,但这个函数总是返回false, errorString表示:

"Cannot open D:/tmp/buf34.txt for input" 

I tried to run this program with administrator privilege but nothing changed. My code is really simple :

我尝试用管理员权限运行这个程序,但没有改变。我的代码非常简单:

  QString source = url.toLocalFile();
  QString destination = _dir.absolutePath()
            + QString("/%1").arg(QFileInfo(source).fileName());
  qDebug()<<"Cp from :" << source << " to : "<< destination;
  QFile file(source);
  qDebug()<<file.copy(destination);
  qDebug()<<file.errorString();

Edit: I have QListView occupied with a QFileSystemModel. I try to drag one file from this ListView to a QLabel. For the QLabel a destination path is set. In drop event I try to copy file.

编辑:我的QListView占用了一个QFileSystemModel。我尝试从这个ListView中拖动一个文件到一个QLabel。对于QLabel,设置了目标路径。在drop事件中,我尝试复制文件。

1 个解决方案

#1


2  

QFile::copy uses QFile::open but overwrites the error message open would give by the unhelpful "Cannot open %1 for input" you got.

::复制使用QFile::打开但覆盖的错误消息打开将会给没有帮助的“不能打开%1输入”。

So, you should try opening the file yourself to get that original error message:

因此,您应该尝试打开文件以获取原始错误消息:

qDebug()<<file.open(QFile::ReadOnly);
qDebug()<<file.errorString();

#1


2  

QFile::copy uses QFile::open but overwrites the error message open would give by the unhelpful "Cannot open %1 for input" you got.

::复制使用QFile::打开但覆盖的错误消息打开将会给没有帮助的“不能打开%1输入”。

So, you should try opening the file yourself to get that original error message:

因此,您应该尝试打开文件以获取原始错误消息:

qDebug()<<file.open(QFile::ReadOnly);
qDebug()<<file.errorString();