用Qt编写的计算文件MD5值的Demo
Dialog.ui
- <?xml version="1.0" encoding="UTF-8"?>
- <ui version="4.0">
- <class>Dialog</class>
- <widget class="QDialog" name="Dialog">
- <property name="geometry">
- <rect>
- <x>0</x>
- <y>0</y>
- <width>329</width>
- <height>161</height>
- </rect>
- </property>
- <property name="windowTitle">
- <string>MD5Hasher</string>
- </property>
- <layout class="QVBoxLayout" name="verticalLayout">
- <item>
- <layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0">
- <item>
- <widget class="QLabel" name="filenameLabel">
- <property name="text">
- <string>文件:</string>
- </property>
- </widget>
- </item>
- <item>
- <widget class="QPushButton" name="chooseFileButton">
- <property name="text">
- <string>选择文件...</string>
- </property>
- </widget>
- </item>
- </layout>
- </item>
- <item>
- <widget class="QLineEdit" name="md5LineEdit">
- <property name="readOnly">
- <bool>true</bool>
- </property>
- </widget>
- </item>
- <item>
- <layout class="QHBoxLayout" name="horizontalLayout_2">
- <item>
- <widget class="QProgressBar" name="md5ProgressBar">
- <property name="value">
- <number>0</number>
- </property>
- </widget>
- </item>
- <item>
- <widget class="QPushButton" name="hashButton">
- <property name="text">
- <string>Hash</string>
- </property>
- </widget>
- </item>
- </layout>
- </item>
- </layout>
- </widget>
- <layoutdefault spacing="6" margin="11"/>
- <resources/>
- <connections/>
- </ui>
MD5Thread.cpp
- #include "MD5Thread.h"
- #include <QCryptographicHash>
- MD5Thread::MD5Thread(const QString &filename) : file(filename)
- {
- qDebug("MD5Thread(const QString &) called");
- }
- MD5Thread::~MD5Thread()
- {
- qDebug("~MD5Thread() called");
- }
- void MD5Thread::run()
- {
- if (file.open(QFile::ReadOnly))
- {
- QCryptographicHash md5(QCryptographicHash::Md5);
- QByteArray buffer;
- qint64 addedDataSize = ;
- while (!(buffer = file.read( * * )).isEmpty())
- {
- md5.addData(buffer);
- emit dataAdded(static_cast<uint>((addedDataSize += buffer.size()) * 100.0 / file.size()));
- }
- emit md5Hashed(md5.result().toHex());
- }
- }
main.cpp
- #include "Dialog.h"
- #include <QApplication>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Dialog w;
- w.show();
- return a.exec();
- }
Dialog.cpp
- #include "Dialog.h"
- #include "ui_Dialog.h"
- #include "MD5Thread.h"
- #include <QFileDialog>
- #include <QCloseEvent>
- #include <QMessageBox>
- Dialog::Dialog(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::Dialog)
- {
- qDebug("Dialog(QWidget *) called");
- ui->setupUi(this);
- }
- Dialog::~Dialog()
- {
- delete ui;
- qDebug("~Dialog() called");
- }
- void Dialog::closeEvent(QCloseEvent *event)
- {
- if (md5Thread)
- {
- if (!md5Thread->isFinished())
- {
- QMessageBox::critical(this, "Tip", "正在计算文件的MD5值!");
- event->ignore();
- }
- }
- }
- void Dialog::on_chooseFileButton_clicked()
- {
- fileToHashMD5 = QFileDialog::getOpenFileName(this, "选择文件");
- if (!fileToHashMD5.isNull())
- ui->filenameLabel->setText(fileToHashMD5);
- }
- void Dialog::on_hashButton_clicked()
- {
- if (!fileToHashMD5.isNull())
- {
- ui->hashButton->setEnabled(false);
- md5Thread = new MD5Thread(fileToHashMD5);
- connect(md5Thread, &MD5Thread::dataAdded, this, &Dialog::onMD5ThreadDataAdded);
- connect(md5Thread, &MD5Thread::md5Hashed, this, &Dialog::onMD5ThreadMD5Hashed);
- connect(md5Thread, &MD5Thread::finished, this, &Dialog::onMD5ThreadFinished);
- md5Thread->start();
- }
- }
- void Dialog::onMD5ThreadDataAdded(uint hashProgress)
- {
- ui->md5ProgressBar->setValue(hashProgress);
- }
- void Dialog::onMD5ThreadMD5Hashed(const QByteArray &md5Data)
- {
- ui->md5LineEdit->setText(md5Data);
- ui->hashButton->setEnabled(true);
- }
- void Dialog::onMD5ThreadFinished()
- {
- md5Thread->deleteLater();
- md5Thread = Q_NULLPTR;
- }
MD5Thread.h
- #ifndef MD5THREAD_H
- #define MD5THREAD_H
- #include <QThread>
- #include <QFile>
- class MD5Thread : public QThread
- {
- Q_OBJECT
- signals:
- void dataAdded(uint hashProgress);
- void md5Hashed(const QByteArray &md5Data);
- public:
- MD5Thread(const QString &filename);
- ~MD5Thread();
- protected:
- void run();
- private:
- QFile file;
- };
- #endif // MD5THREAD_H
Dialog.h
- #ifndef DIALOG_H
- #define DIALOG_H
- #include <QDialog>
- namespace Ui {
- class Dialog;
- }
- class MD5Thread;
- class Dialog : public QDialog
- {
- Q_OBJECT
- public:
- explicit Dialog(QWidget *parent = );
- ~Dialog();
- protected:
- void closeEvent(QCloseEvent *event);
- private:
- Ui::Dialog *ui;
- MD5Thread *md5Thread = Q_NULLPTR;
- QString fileToHashMD5;
- private slots:
- void on_chooseFileButton_clicked();
- void on_hashButton_clicked();
- void onMD5ThreadDataAdded(uint hashProgress);
- void onMD5ThreadMD5Hashed(const QByteArray &md5Data);
- void onMD5ThreadFinished();
- };
- #endif // DIALOG_H
用Qt编写的计算文件MD5值的Demo的更多相关文章
- Java计算文件MD5值(支持大文件)
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.securit ...
- python 计算文件md5值
md5是一种常见不可逆加密算法,使用简单,计算速度快,在很多场景下都会用到,比如:给用户上传的文件命名,数据库中保存的用户密码,下载文件后检验文件是否正确等.下面讲解在python中如何使用md5算法 ...
- Java计算文件MD5值代码
原文:http://www.open-open.com/code/view/1424930488031 import java.io.File; import java.io.FileInputStr ...
- Python计算文件MD5值
import hashlib def fileMD5(filename): m = hashlib.md5() #md5计算 #m = hashlib.sha1() #sha1计算 #m = hash ...
- java计算过G文件md5 值计算
package io.bigdata; import java.io.File; import java.io.FileInputStream; import java.io.IOException; ...
- javascript 计算文件MD5 浏览器 javascript读取文件内容
原则上说,浏览器是一个不安全的环境.早期浏览器的内容是静态的,用户上网冲浪,一般就是拉取网页查看.后来,随着互联网的发展,浏览器提供了非常丰富的用户交互功能.从早期的表单交互,到现在的websocke ...
- QT 获取文件MD5值
/* 方法1 */ QFile theFile(fileNamePath); theFile.open(QIODevice::ReadOnly); QByteArray ba = QCryptogra ...
- 基于js-spark-md5前端js类库,快速获取文件Md5值
js-spark-md5是歪果仁开发的东西,有点多,但是我们只要一个js文件即可,具体类包我存在自己的oschina上,下载地址:https://git.oschina.net/jianqingwan ...
- MD5工具类,提供字符串MD5加密、文件MD5值获取(校验)功能
MD5工具类,提供字符串MD5加密(校验).文件MD5值获取(校验)功能 : package com.yzu.utils; import java.io.File; import java.io.Fi ...
随机推荐
- sqlserver查询表大小
IF OBJECT_ID('tempdb..#TB_TEMP_SPACE') IS NOT NULL DROP TABLE #TB_TEMP_SPACE GO CREATE TABLE #TB_TEM ...
- 11g自动分区超过最大限制
公司业务系统一张表按时间每天分区 写入数据时报错:ORA-14300: 分区关键字映射到超出允许的最大分区数的分区 ORA-14300: partitioning key maps to a part ...
- BZOJ 2331 [SCOI2011]地板 ——插头DP
[题目分析] 经典题目,插头DP. switch 套 switch 代码瞬间清爽了. [代码] #include <cstdio> #include <cstring> #in ...
- BZOJ1925 [Sdoi2010]地精部落 【dp】
题目 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H可分 为从左到右的 N 段,每段有一个独一无二的高度 Hi,其中Hi是1到N ...
- 【单调队列】poj 2823 Sliding Window
http://poj.org/problem?id=2823 [题意] 给定一个长度为n的序列,求长度为k的滑窗内的最大值和最小值 [思路] 裸的单调队列 注意用C++提交,不然会T,orz我用G++ ...
- Java中的自动类型转换
Java里所有的数值型变量可以进行类型转换,这个大家都知道,应该不需要详细解释为什么. 2 在说明自动类型转换之前必须理解这样一个原则“表数范围小的可以向表数范围大的进行自动类型转换” 3 关于jav ...
- javaScriptCore 实战与小结
源码在这,看不懂的直接撸源码就行,转载声明出处 原生调用JS的大致流程,做了个思维简图 这是代码流程 // JS数据 func getJSVar() { let context: JSContex ...
- uva 10140 素数筛选(两次)
#include<iostream> #include<cstring> #include<cmath> #include<cstdio> using ...
- 巴蜀2904 MMT数
Description FF博士最近在研究MMT数. 如果对于一个数n,存在gcd(n,x)<>1并且n mod x<>0 那么x叫做n的MMT数,显然这样的数可以有无限个. ...
- 【BZOJ1208】宠物收养所(splay)
题意:见题面 思路:因为每个时刻要么全是人要么全是宠物,所以可以一棵splay解决 维护单点插入,单点删除,求前驱,求后继即可 ..,..]of longint; num,fa:..]of longi ...