我的主页

快递查询 :http://m.kuaidi100.com     爱快递查询 :http://www.aikuaidi.cn/              

http://www.qtcn.org/bbs/u/168050  [收藏] [复制]

w642833823

  • 10

    关注

  • 67

    粉丝

  • 42

    访客

  • 等级:新手上路
  • 总积分:43
  • 保密,2011-12-07

最后登录:2024-04-26

更多资料

日志

记录QT实现文件夹及文件复制到指定路径中

2021-08-16 22:35
  1. //来自》》》https://www.cnblogs.com/liuyunfeifei/archive/2013/02/21/2919960.html

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

  1. //复制文件夹
    bool Widget::copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist)
    {
        QDir sourceDir(fromDir);
        QDir targetDir(toDir);
        if(!targetDir.exists()){    /**< 如果目标目录不存在,则进行创建 */
            if(!targetDir.mkdir(targetDir.absolutePath()))
                return false;
        }
        QFileInfoList fileInfoList = sourceDir.entryInfoList();
        foreach(QFileInfo fileInfo, fileInfoList){
            if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
                continue;
            if(fileInfo.isDir()){    /**< 当为目录时,递归的进行copy */
                if(!copyDirectoryFiles(fileInfo.filePath(),
                    targetDir.filePath(fileInfo.fileName()),
                    coverFileIfExist))
                    return false;
            }
            else{            /**< 当允许覆盖操作时,将旧文件进行删除操作 */
                if(coverFileIfExist && targetDir.exists(fileInfo.fileName())){
                    targetDir.remove(fileInfo.fileName());
                }
                /// 进行文件copy
                if(!QFile::copy(fileInfo.filePath(),
                    targetDir.filePath(fileInfo.fileName()))){
                        return false;
                }
            }
        }
        return true;
    }


使用
  1. //使用
    //复制文件
    copyFileToPath(":/res/123.mp3" ,目标位置, true);
    //复制文件夹
    copyDirectoryFiles(":/res",SOUND_PATH,true);





分类:QT|回复:0|浏览:868|全站可见|转载
 

Powered by phpwind v8.7 Certificate Copyright Time now is:04-26 21:42
©2005-2016 QTCN开发网 版权所有 Gzip disabled