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的更多相关文章

  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. Mysql 主外键与索引之间的区别和联系

    系数据库依赖于主键,它是数据库物理模式的基石.主键在物理层面上只有两个用途: 惟一地标识一行. 作为一个可以被外键有效引用的对象. 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部 ...

  2. 洛谷P3759 - [TJOI2017]不勤劳的图书管理员

    Portal Description 给出一个\(1..n(n\leq5\times10^4)\)的排列\(\{a_n\}\)和数列\(\{w_n\}(w_i\leq10^5)\),进行\(m(m\l ...

  3. 刷题总结——spoj1812(后缀自动机+DP)

    题目: A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is t ...

  4. 【译】NCCloud: Applying Network Coding for the Storage Repair in a Cloud-of-Clouds

    NCCloud:多云存储设备下存储修复的网络编码 Yuchong Hu, Henry C. H. Chen, Patrick P. C. Lee, Yang Tang  摘要:近年来的研究提出通过条带 ...

  5. Numpy 布尔型数组

    一  给定一个列表,返回大于10的元素. 在python中,有两种方法.一种方法是循环遍历,第二种方法是使用内置函数filter() 在数组中,有更为简单的方法.即布尔型索引 布尔型索引: 将同样大小 ...

  6. 标准C程序设计七---07

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  7. Centos7安装完成后一些基本操作

    1.基本操作一:主机名 # centos7有一个新的修改主机名的命令hostnamectl hostnamectl set-hostname --static www.node1.com # 有些命令 ...

  8. codevs3249搭积木

    3249 搭积木  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description Petya有一个A×B×C的长方体积木,积 ...

  9. 注解@RequestMapping value 用法

    本文引自:https://blog.csdn.net/qq_33811662/article/details/80864784 RequestMapping是一个用来处理请求地址映射的注解,可用于类. ...

  10. 航空售票系统设计分析(Markdownpad2图片服务器上传无法显示)

    一.体系结构设计 1.系统原型图 2.体系结构环境图 3.构建结构图 二.人机交互界面设计 1.用户分析结果及建议 本次分析的主要目标关注用户评论反馈,对反馈进行归纳,设计出用户喜欢的界面样式.用户的 ...