qt 拷贝文件设置进度条

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
 * @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;
}

  

posted @   wjbooks  阅读(2278)  评论(3编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2017-11-05 我喜欢的人
点击右上角即可分享
微信分享提示