C++ Transfer Files From Qt to External USB Drive -


i new in qt , need in transferring files specific path of local machine external usb drive.

copying single file

you can use qfile::copy.

qfile::copy(srcpath, dstpath); 

note: function doesn't overwrite files, must delete previous files if exist:

if (qfile::exist(dstpath)) qfile::remove(dstpath); 

if need show user interface source , destination paths, can use qfiledialog's methods that. example:

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 (*.*)"); // asks user overwriting existing files   if (dstpath.isnull()) return false;    if (qfile::exist(dstpath))     if (!qfile::remove(dstpath)) return false; // couldn't delete file       // write-protected or insufficient privileges    return qfile::copy(srcpath, dstpath); } 

copying whole content of directory

i'm extending answer case srcpath directory. must done manually , recursively. here code it, without error checking simplicity. must in charge of choosing right method (take @ qfileinfo::isfile ideas.

void recursivecopy(const qstring& srcpath, const qstring& dstpath) {   qdir().mkpath(dstpath); // 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);   } } 

if need ask directory, can use qfiledialog::getexistingdirectory.

final remarks

both methods assume srcpath exists. if used qfiledialog methods highly probable exists (highly probable because not atomic operation , directory or file may deleted or renamed between dialog , copy operation, different issue).


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -