Qt制作有进度条的拷贝文件夹和文件的小Demo

效果图:



核心代码:

#ifndef SFILECOPY_H
#define SFILECOPY_H

#include <QObject>
#include <QDir>

class SFileCopy : public QObject
{
    Q_OBJECT
public:
    explicit SFileCopy(QObject *parent = 0);
    ~SFileCopy();

    //拷贝文件:
    bool copyFileToPath(QString sourceDir ,QString toDir, bool coverFileIfExist);

    //拷贝文件夹:
    bool copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist);
signals:
    void sigCopyDirStation(float num);
    void sigCopyDirOver();
private:
    QDir * m_createfile = Q_NULLPTR;
    float m_total = 0;
    float m_value = 0;
    bool m_firstRead = true;
};

#endif // SFILECOPY_H

#include "sfilecopy.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QDebug>

SFileCopy::SFileCopy(QObject *parent) : QObject(parent)
{
    m_createfile = new QDir();
}

SFileCopy::~SFileCopy()
{
    if(m_createfile) {
        m_createfile = Q_NULLPTR;
        delete m_createfile;
    }
}

bool SFileCopy::copyFileToPath(QString sourceDir, QString toDir, bool coverFileIfExist)
{
    toDir.replace("\\","/");
    if (sourceDir == toDir){
        return true;
    }
    if (!QFile::exists(sourceDir)){
        return false;
    }
    bool exist = m_createfile->exists(toDir);
    if (exist){
        if(coverFileIfExist){
            m_createfile->remove(toDir);
        }
    }//end if

    if(!QFile::copy(sourceDir, toDir)) {
        return false;
    }
    return true;
}

bool SFileCopy::copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist)
{
    QDir sourceDir(fromDir);
    QDir targetDir(toDir);
    qDebug() << "copyDirectoryFiles:" << fromDir << toDir;
    if(!targetDir.exists()){    /**< 如果目标目录不存在,则进行创建 */
        if(!targetDir.mkdir(targetDir.absolutePath())) {
            return false;
        }
    }
    QFileInfoList fileInfoList = sourceDir.entryInfoList();

    if(m_firstRead) {
        int isfileTMP = 0;
        qDebug() << "a copyDirectoryFiles:" << fileInfoList.count();
        foreach(QFileInfo fileInfo, fileInfoList){
            if(fileInfo.isFile()) {
                isfileTMP++;
            }
        }
        m_total = fileInfoList.count() - 2 - isfileTMP; // 2为.和..
        m_value = 0;
        m_firstRead = false;
        qDebug() << "a copyDirectoryFiles:" << fileInfoList.count() <<m_total << isfileTMP;
        emit sigCopyDirStation(m_value/m_total);
        if(m_value == m_total) {
            m_firstRead = true;
            emit sigCopyDirStation(1);
            emit sigCopyDirOver();
        }
    } else {
        m_value++;
        if(m_value == m_total) {
            m_firstRead = true;
            emit sigCopyDirOver();
        }
        qDebug() << "a copyDirectoryFiles:" << m_value <<m_total;
        emit sigCopyDirStation(m_value/m_total);
    }
    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;
}

调用:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDateTime>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_file = new SFileCopy(this);
    connect(m_file, &SFileCopy::sigCopyDirStation,[=](float num){
        ui->progressBar->setValue(100*num);
    });
    connect(m_file, &SFileCopy::sigCopyDirOver,[=](){
        ui->label->setText("Over");
    });
    m_fileDialog = new QFileDialog(this);
    m_fileDialog->setWindowTitle(tr("Open"));
    m_fileDialog->setDirectory(".");
    m_fileDialog->setFileMode(QFileDialog::DirectoryOnly);
    m_fileDialog->hide();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    ui->label->setText("");
    m_fileDialog->show();
    QStringList files;
    if(m_fileDialog->exec() == QDialog::Accepted) {
        files = m_fileDialog->selectedFiles();
    }
    QString fTMP = files.first();
    QString newTMP = fTMP + QDateTime::currentDateTime().toString("yyyy-MM-dd");
    m_file->copyDirectoryFiles(fTMP, newTMP , true);
}

注:

这个小Demo仅供参考~~


©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页