2
votes

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

"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.

1
How about adding qDebug() << "source exists: " << QFileInfo(source).exists(); to see if the source file exists? - user362638
No I'm sure that source file doesn't exist in destination. The error says that source file couldn't be opened. - s4eed
How did the source file get there? Is it written by some other program which still could have the file open? - user362638
I set a QFileSystemModel model with a path for a listView. Then I drag one file from this listView to a label(Destination is set for this label ) . At the end of drop I try to copy the file selected from listView to label's directory ! - s4eed
@saeed I guess this file is opened by another QFile::open, if not: "source" exists? You may not copying one file that does not exist. If there is, you are trying to record in a directory with permissions (Windows Vista, Windows 7, etc.)? - Guilherme Nascimento

1 Answers

2
votes

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

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

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