[QT学习]拷贝文件

时间:2023-03-15 23:10:17

sourceDir源绝对路径,toDir目标绝对路径

 //拷贝文件:
bool FileOperation::copyFileToPath(QString sourceDir ,QString toDir, bool coverFileIfExist)
{
toDir.replace("\\","/");
if (sourceDir == toDir){
return true;
}
if (!QFile::exists(sourceDir)){
return false;
}
QFileInfo fi(toDir);
QString dirPath=fi.absolutePath();//取路径
QDir *createfile = new QDir;
bool exist=createfile->exists(dirPath);
if(!exist)
{
createfile->mkdir(dirPath);
}
exist = createfile->exists(toDir);
if (exist){
if(coverFileIfExist){
createfile->remove(toDir);
}
}//end if if(!QFile::copy(sourceDir, toDir))
{
return false;
}
return true;
}