Dialog.ui

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>Dialog</class>
  4. <widget class="QDialog" name="Dialog">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>329</width>
  10. <height>161</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>MD5Hasher</string>
  15. </property>
  16. <layout class="QVBoxLayout" name="verticalLayout">
  17. <item>
  18. <layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0">
  19. <item>
  20. <widget class="QLabel" name="filenameLabel">
  21. <property name="text">
  22. <string>文件:</string>
  23. </property>
  24. </widget>
  25. </item>
  26. <item>
  27. <widget class="QPushButton" name="chooseFileButton">
  28. <property name="text">
  29. <string>选择文件...</string>
  30. </property>
  31. </widget>
  32. </item>
  33. </layout>
  34. </item>
  35. <item>
  36. <widget class="QLineEdit" name="md5LineEdit">
  37. <property name="readOnly">
  38. <bool>true</bool>
  39. </property>
  40. </widget>
  41. </item>
  42. <item>
  43. <layout class="QHBoxLayout" name="horizontalLayout_2">
  44. <item>
  45. <widget class="QProgressBar" name="md5ProgressBar">
  46. <property name="value">
  47. <number>0</number>
  48. </property>
  49. </widget>
  50. </item>
  51. <item>
  52. <widget class="QPushButton" name="hashButton">
  53. <property name="text">
  54. <string>Hash</string>
  55. </property>
  56. </widget>
  57. </item>
  58. </layout>
  59. </item>
  60. </layout>
  61. </widget>
  62. <layoutdefault spacing="6" margin="11"/>
  63. <resources/>
  64. <connections/>
  65. </ui>

MD5Thread.cpp

  1. #include "MD5Thread.h"
  2. #include <QCryptographicHash>
  3.  
  4. MD5Thread::MD5Thread(const QString &filename) : file(filename)
  5. {
  6. qDebug("MD5Thread(const QString &) called");
  7. }
  8.  
  9. MD5Thread::~MD5Thread()
  10. {
  11. qDebug("~MD5Thread() called");
  12. }
  13.  
  14. void MD5Thread::run()
  15. {
  16. if (file.open(QFile::ReadOnly))
  17. {
  18. QCryptographicHash md5(QCryptographicHash::Md5);
  19. QByteArray buffer;
  20. qint64 addedDataSize = ;
  21. while (!(buffer = file.read( * * )).isEmpty())
  22. {
  23. md5.addData(buffer);
  24. emit dataAdded(static_cast<uint>((addedDataSize += buffer.size()) * 100.0 / file.size()));
  25. }
  26. emit md5Hashed(md5.result().toHex());
  27. }
  28. }

main.cpp

  1. #include "Dialog.h"
  2. #include <QApplication>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication a(argc, argv);
  7. Dialog w;
  8. w.show();
  9. return a.exec();
  10. }

Dialog.cpp

  1. #include "Dialog.h"
  2. #include "ui_Dialog.h"
  3. #include "MD5Thread.h"
  4. #include <QFileDialog>
  5. #include <QCloseEvent>
  6. #include <QMessageBox>
  7.  
  8. Dialog::Dialog(QWidget *parent) :
  9. QDialog(parent),
  10. ui(new Ui::Dialog)
  11. {
  12. qDebug("Dialog(QWidget *) called");
  13. ui->setupUi(this);
  14. }
  15.  
  16. Dialog::~Dialog()
  17. {
  18. delete ui;
  19. qDebug("~Dialog() called");
  20. }
  21.  
  22. void Dialog::closeEvent(QCloseEvent *event)
  23. {
  24. if (md5Thread)
  25. {
  26. if (!md5Thread->isFinished())
  27. {
  28. QMessageBox::critical(this, "Tip", "正在计算文件的MD5值!");
  29. event->ignore();
  30. }
  31. }
  32. }
  33.  
  34. void Dialog::on_chooseFileButton_clicked()
  35. {
  36. fileToHashMD5 = QFileDialog::getOpenFileName(this, "选择文件");
  37. if (!fileToHashMD5.isNull())
  38. ui->filenameLabel->setText(fileToHashMD5);
  39. }
  40.  
  41. void Dialog::on_hashButton_clicked()
  42. {
  43. if (!fileToHashMD5.isNull())
  44. {
  45. ui->hashButton->setEnabled(false);
  46. md5Thread = new MD5Thread(fileToHashMD5);
  47. connect(md5Thread, &MD5Thread::dataAdded, this, &Dialog::onMD5ThreadDataAdded);
  48. connect(md5Thread, &MD5Thread::md5Hashed, this, &Dialog::onMD5ThreadMD5Hashed);
  49. connect(md5Thread, &MD5Thread::finished, this, &Dialog::onMD5ThreadFinished);
  50. md5Thread->start();
  51. }
  52. }
  53.  
  54. void Dialog::onMD5ThreadDataAdded(uint hashProgress)
  55. {
  56. ui->md5ProgressBar->setValue(hashProgress);
  57. }
  58.  
  59. void Dialog::onMD5ThreadMD5Hashed(const QByteArray &md5Data)
  60. {
  61. ui->md5LineEdit->setText(md5Data);
  62. ui->hashButton->setEnabled(true);
  63. }
  64.  
  65. void Dialog::onMD5ThreadFinished()
  66. {
  67. md5Thread->deleteLater();
  68. md5Thread = Q_NULLPTR;
  69. }

MD5Thread.h

  1. #ifndef MD5THREAD_H
  2. #define MD5THREAD_H
  3.  
  4. #include <QThread>
  5. #include <QFile>
  6. class MD5Thread : public QThread
  7. {
  8. Q_OBJECT
  9. signals:
  10. void dataAdded(uint hashProgress);
  11. void md5Hashed(const QByteArray &md5Data);
  12. public:
  13. MD5Thread(const QString &filename);
  14. ~MD5Thread();
  15. protected:
  16. void run();
  17. private:
  18. QFile file;
  19. };
  20.  
  21. #endif // MD5THREAD_H

Dialog.h

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3.  
  4. #include <QDialog>
  5.  
  6. namespace Ui {
  7. class Dialog;
  8. }
  9.  
  10. class MD5Thread;
  11.  
  12. class Dialog : public QDialog
  13. {
  14. Q_OBJECT
  15.  
  16. public:
  17. explicit Dialog(QWidget *parent = );
  18. ~Dialog();
  19.  
  20. protected:
  21. void closeEvent(QCloseEvent *event);
  22.  
  23. private:
  24. Ui::Dialog *ui;
  25. MD5Thread *md5Thread = Q_NULLPTR;
  26. QString fileToHashMD5;
  27.  
  28. private slots:
  29. void on_chooseFileButton_clicked();
  30. void on_hashButton_clicked();
  31. void onMD5ThreadDataAdded(uint hashProgress);
  32. void onMD5ThreadMD5Hashed(const QByteArray &md5Data);
  33. void onMD5ThreadFinished();
  34.  
  35. };
  36.  
  37. #endif // DIALOG_H

用Qt编写的计算文件MD5值的Demo的更多相关文章

  1. Java计算文件MD5值(支持大文件)

    import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.securit ...

  2. python 计算文件md5值

    md5是一种常见不可逆加密算法,使用简单,计算速度快,在很多场景下都会用到,比如:给用户上传的文件命名,数据库中保存的用户密码,下载文件后检验文件是否正确等.下面讲解在python中如何使用md5算法 ...

  3. Java计算文件MD5值代码

    原文:http://www.open-open.com/code/view/1424930488031 import java.io.File; import java.io.FileInputStr ...

  4. Python计算文件MD5值

    import hashlib def fileMD5(filename): m = hashlib.md5() #md5计算 #m = hashlib.sha1() #sha1计算 #m = hash ...

  5. java计算过G文件md5 值计算

    package io.bigdata; import java.io.File; import java.io.FileInputStream; import java.io.IOException; ...

  6. javascript 计算文件MD5 浏览器 javascript读取文件内容

    原则上说,浏览器是一个不安全的环境.早期浏览器的内容是静态的,用户上网冲浪,一般就是拉取网页查看.后来,随着互联网的发展,浏览器提供了非常丰富的用户交互功能.从早期的表单交互,到现在的websocke ...

  7. QT 获取文件MD5值

    /* 方法1 */ QFile theFile(fileNamePath); theFile.open(QIODevice::ReadOnly); QByteArray ba = QCryptogra ...

  8. 基于js-spark-md5前端js类库,快速获取文件Md5值

    js-spark-md5是歪果仁开发的东西,有点多,但是我们只要一个js文件即可,具体类包我存在自己的oschina上,下载地址:https://git.oschina.net/jianqingwan ...

  9. MD5工具类,提供字符串MD5加密、文件MD5值获取(校验)功能

    MD5工具类,提供字符串MD5加密(校验).文件MD5值获取(校验)功能 : package com.yzu.utils; import java.io.File; import java.io.Fi ...

随机推荐

  1. sqlserver查询表大小

    IF OBJECT_ID('tempdb..#TB_TEMP_SPACE') IS NOT NULL DROP TABLE #TB_TEMP_SPACE GO CREATE TABLE #TB_TEM ...

  2. 11g自动分区超过最大限制

    公司业务系统一张表按时间每天分区 写入数据时报错:ORA-14300: 分区关键字映射到超出允许的最大分区数的分区 ORA-14300: partitioning key maps to a part ...

  3. BZOJ 2331 [SCOI2011]地板 ——插头DP

    [题目分析] 经典题目,插头DP. switch 套 switch 代码瞬间清爽了. [代码] #include <cstdio> #include <cstring> #in ...

  4. BZOJ1925 [Sdoi2010]地精部落 【dp】

    题目 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H可分 为从左到右的 N 段,每段有一个独一无二的高度 Hi,其中Hi是1到N ...

  5. 【单调队列】poj 2823 Sliding Window

    http://poj.org/problem?id=2823 [题意] 给定一个长度为n的序列,求长度为k的滑窗内的最大值和最小值 [思路] 裸的单调队列 注意用C++提交,不然会T,orz我用G++ ...

  6. Java中的自动类型转换

    Java里所有的数值型变量可以进行类型转换,这个大家都知道,应该不需要详细解释为什么. 2 在说明自动类型转换之前必须理解这样一个原则“表数范围小的可以向表数范围大的进行自动类型转换” 3 关于jav ...

  7. javaScriptCore 实战与小结

      源码在这,看不懂的直接撸源码就行,转载声明出处 原生调用JS的大致流程,做了个思维简图 这是代码流程 // JS数据 func getJSVar() { let context: JSContex ...

  8. uva 10140 素数筛选(两次)

    #include<iostream> #include<cstring> #include<cmath> #include<cstdio> using ...

  9. 巴蜀2904 MMT数

    Description FF博士最近在研究MMT数. 如果对于一个数n,存在gcd(n,x)<>1并且n mod x<>0 那么x叫做n的MMT数,显然这样的数可以有无限个. ...

  10. 【BZOJ1208】宠物收养所(splay)

    题意:见题面 思路:因为每个时刻要么全是人要么全是宠物,所以可以一棵splay解决 维护单点插入,单点删除,求前驱,求后继即可 ..,..]of longint; num,fa:..]of longi ...