Will QFile::copy create create a copy of the file or move the contents from one file to another?

user1065969 picture user1065969 · May 11, 2012 · Viewed 18.3k times · Source

I am trying to copy a file from one location to another( in a device) using C++/Qt

I tried QFile::copy("path1/file","path2");

I want to copy the file in path1 to path2. path2 does not have the file.

I just want to know if this is the right way because the above code does not seem to work.

Also, should I do a file open before I try to copy? Need help!

Answer

Joel Rondeau picture Joel Rondeau · May 11, 2012

If you want to copy path1/file into path2 with the same file name, you'll want to do:

QFile::copy("path1/file", "path2/file");

Copy allows you to change the name of the file. Example:

QFile::copy("path1/file1", "path1/file2");

Which is why you need to include the file name both times. Also, it is not necessary to open the file first. And to answer the title question, it copies the file. QFile::rename() moves the contents.