Qt实现复制文件线程要点

Qt实现复制文件线程要点

1.文件复制

2.文件夹的创建

思路:获取源路径下文件及文件夹相对于路径的位置,利用线程进行相关操作。

                          

       根据上图:


 
 
  1. // 文件位置集合
  2. QStringList fileList;
  3. fileList << "3.txt" << "4.txt" << "1/5.txt";
  4. // 文件夹集合
  5. QStringList dirList;
  6. dirList<< "1" << "2";

 
 
  1. #ifndef COPYTHREAD_H
  2. #define COPYTHREAD_H
  3. #include <QThread>
  4. #include <QString>
  5. #include <QSet>
  6. // 复制仓库
  7. typedef struct sourceRepositoryStu
  8. {
  9. QStringList fileList; // 复制路径下的文件相对名字及位置
  10. QStringList dirList; // 复制路径下的文件夹
  11. }SourceRepository;
  12. // 用法
  13. //QStringList fileList;
  14. //QStringList dirList;
  15. //fileList << "updater.cpp" << "updater.h" << "packager.cpp" << "updater/downloadmanager.cpp";
  16. //dirList << "updater" << "packager";
  17. //SourceRepository stu;
  18. //stu.fileList = fileList;
  19. //stu.dirList = dirList;
  20. //CopyThread test(stu, "E:\\Test\\src", "E:\\Copy\\src");
  21. //test.start();
  22. /**
  23. * @brief 复制线程
  24. *
  25. * 包括复制文件及建立文件夹
  26. */
  27. class CopyThread : public QThread
  28. {
  29. Q_OBJECT
  30. public:
  31. CopyThread( const SourceRepository &sourceRepository,
  32. const QString &sourceDir,
  33. const QString &destinationDir, QObject *parent = 0);
  34. signals:
  35. void warning(const QString &warning);
  36. void progression(int copiedFileCount, int totalFileCount);
  37. void copyFinished(const QString &errorString);
  38. protected:
  39. void run() Q_DECL_OVERRIDE;
  40. private:
  41. SourceRepository m_sourceRepository; // 源路径下的文件及文件夹名字
  42. QString m_destinationDir; // 目标路径
  43. QString m_sourceDir; // 源路径
  44. };
  45. #endif // COPYTHREAD_H

 
 
  1. #include "CopyThread.h"
  2. #include <QFile>
  3. #include <QFileInfo>
  4. #include <QDir>
  5. #include <QDebug>
  6. QString cleanPath( const QString &pathName, bool separatorAtEnd)
  7. {
  8. if (pathName.isEmpty())
  9. return pathName;
  10. QString cleaned = pathName;
  11. cleaned.replace( '\\', '/');
  12. if (separatorAtEnd)
  13. {
  14. if (!cleaned.endsWith( '/'))
  15. cleaned += '/';
  16. }
  17. else
  18. {
  19. if (cleaned.endsWith( '/'))
  20. cleaned.chop( 1);
  21. }
  22. return cleaned;
  23. }
  24. CopyThread::CopyThread( const SourceRepository &sourceRepository,
  25. const QString &sourceDir,
  26. const QString &destinationDir, QObject * parent) :
  27. QThread( parent), m_sourceRepository(sourceRepository)
  28. {
  29. m_sourceDir = cleanPath(sourceDir, true);
  30. m_destinationDir = cleanPath(destinationDir, true);
  31. qWarning() << m_sourceDir << m_destinationDir;
  32. }
  33. void CopyThread::run()
  34. {
  35. try
  36. {
  37. QSet<QString> sourcefiles = m_sourceRepository.fileList.toSet();
  38. QSet<QString> dirfiles = m_sourceRepository.dirList.toSet();
  39. int i = 0;
  40. QFileInfo destFileInfo;
  41. QDir dir;
  42. QString src, dest;
  43. foreach( const QString &dirname, dirfiles)
  44. {
  45. dest = m_destinationDir + dirname;
  46. if (!dir.mkpath(dest))
  47. qWarning() << tr( "Unable to create path %1").arg(dest);
  48. }
  49. foreach( const QString &filename, sourcefiles)
  50. {
  51. src = m_sourceDir + filename;
  52. dest = m_destinationDir + filename;
  53. destFileInfo.setFile(dest);
  54. if (!dir.mkpath(destFileInfo.absolutePath()))
  55. qWarning() << tr( "Unable to create path %1").arg(destFileInfo.absolutePath());
  56. if(QFile::exists(dest) && !QFile::remove(dest))
  57. qWarning() << tr( "Unable to remove %1").arg(dest);
  58. if(!QFile::copy(src, dest))
  59. qWarning() << tr( "Unable to copy %1 to %2").arg(src, dest);
  60. emit progression(++i, sourcefiles.size());
  61. }
  62. emit copyFinished(QString());
  63. }
  64. catch(std::exception &msg)
  65. {
  66. emit copyFinished(msg.what());
  67. }
  68. }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值