qt 拷贝文件设置进度条

时间:2022-09-29 06:45:12
/**
* @brief FuncModuleWin::copyFile
* @param fromFIleName 优盘里面的文件
* @param toFileName 拷贝到/bin里面的启动文件
* @return
*/
bool FuncModuleWin::copyFile(const QString &fromFIleName, const QString &toFileName)
{
char* byteTemp = new char[4096];//字节数组
int fileSize = 0;
int totalCopySize = 0;
QFile tofile;
ui->progressBar_copy->setValue(0);
tofile.setFileName(toFileName);
QDataStream out(&tofile);
out.setVersion(QDataStream::Qt_4_8); QFile fromfile;
fromfile.setFileName(fromFIleName);
if(!fromfile.open(QIODevice::ReadOnly)){
qDebug() << "open fromfile failed!!!";
return false;
}
fileSize = fromfile.size();
QDataStream in(&fromfile); in.setVersion(QDataStream::Qt_4_8);
ui->progressBar_copy->setRange(0, fileSize);
while(!in.atEnd())
{
int readSize = 0;
//读入字节数组,返回读取的字节数量,如果小于4096,则到了文件尾
readSize = in.readRawData(byteTemp, 4096);
out.writeRawData(byteTemp, readSize);
totalCopySize += readSize;
ui->progressBar_copy->setValue(totalCopySize);
}
if(totalCopySize == fileSize){
tofile.setPermissions(QFile::ExeUser);
return true;
}
else
return false;
}