QT开发一款MD5校验工具

  • Post author:
  • Post category:其他


这是一款基于QT开发的MD5校验工具,在这你可以学习到界面UI的设计方法,以及QT应对字符串的处理方法。以及如何去使用QT进行多线程的开发。

实现后的程序运行截图:

MD5.pro工程文件

#-------------------------------------------------
#
# Project created by QtCreator 2020-09-25T14:41:18
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = MD5
TEMPLATE = app

RC_ICONS = MD5.ico

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        md5test.cpp \
        md5thread.cpp

HEADERS += \
        md5test.h \
        md5thread.h

FORMS +=

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    image.qrc

md5Test.h文件

#ifndef MD5TEST_H
#define MD5TEST_H

#include <QWidget>
#include <QPushButton>
#include <qgroupbox.h>
#include <qlineedit.h>
#include <qlabel.h>
#include <qgridlayout.h>
#include <qtablewidget.h>
#include <QVBoxLayout>
#include <QFileInfo>
#include <qdebug.h>
#include <QErrorMessage>
#include <QFileDialog>
#include <qdir.h>
#include <qerrormessage.h>
#include <QDirIterator>
#include <QEvent>
#include <QPaintEvent>
#include <QHBoxLayout>
#include <QTreeWidget>
#include <QStackedWidget>
#include <QProgressBar>
#include "md5thread.h"

class md5Test : public QWidget
{
    Q_OBJECT
signals:
    void md5ErrorSignal(QString);
    void openMd5Thread();
    void sendPath(QString);
private slots:
    void searchDir();
    void startTest();
    void checkself(QTreeWidgetItem* ,int);
    void threadFinished();

    void rcevData_1(QString ,QString);//接收文件对应MD5
    void recvData_5(QString,QString,QDateTime);//接收text文件中获取的MD5
    void recvData_3(QString,QString);//接收不匹配的MD5
    void recvData_4(QString,QString);//接收新的MD5
    void recvData_2(QString,QString);//接收匹配的MD5
public:
    explicit md5Test(QWidget *parent = nullptr);
    ~md5Test();

    void viewFileMd5();//文件及对应MD5
    void viewTextMd5();//txt文件中的md5
    void viewMismatchMd5();//显示不匹配MD5表格
    void viewNewMd5();//显示新的MD5表格
    void viewMatchMd5();//显示匹配MD5表格
    void initTable();//初始化表格
    void clearTableRow();//删除所有行
private:
    QTableWidget *tablewidget;
    QTableWidget *tablewidget_1;//匹配的
    QTableWidget *tablewidget_2;//不匹配的
    QTableWidget *tablewidget_3;//新的
    QTableWidget *tablewidget_4;//抓取的

    QPushButton *pushbutton;
    QPushButton *testbtn;
    QGroupBox *groupbox;
    QLineEdit *searchPathLineedit;
    QLineEdit *mismatchMd5NumLineEdit;
    QLineEdit *matchMd5NumLineEdit;
    QLineEdit *newMd5NumLineEdit;
    QLineEdit *sumMd5NumLineEdit;

    QLabel *searchPathLabel;
    QLabel *mismatchMd5Label;
    QLabel *matchMd5Label;
    QLabel *newMd5Label;
    QLabel *sumMd5Label;
    QLabel *mainwindowtitle;
    QVBoxLayout *layout;
    QGridLayout *layout_2;

    QString msg;
    QString path;

    QTreeWidget *treewidget;
    QHBoxLayout *hlayout;
    QStackedWidget *stackwidget;
    QWidget *mainwidget;

    QProgressBar *progressbar;

    md5Thread m_thread;

    bool ok = false;

    int provalue = 0;

    int row,row_1,row_2,row_3,row_4;
};

#endif // MD5TEST_H

md5Test.cpp

#include "md5test.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QHBoxLayout>
#include <QHeaderView>
#define WINDOWTITLE "MD5检验"
md5Test::md5Test(QWidget *parent) :
    QWidget(parent)
{
    int wW = 1200;
    int hW = 800;
    this->resize(wW,hW);

    setWindowTitle(WINDOWTITLE);
    setWindowIcon(QIcon(":/new/prefix1/images/mainwidget.png"));
    //setWindowFlag(Qt::FramelessWindowHint,true);
    row = 0;
    row_1 = 0;
    row_2 = 0;
    row_3 = 0;
    row_4 = 0;
    QFont font;
    font.setFamily("黑体");
    font.setPixelSize(16);
    setFont(font);

    stackwidget = new QStackedWidget(this);

    mainwidget = new QWidget(stackwidget);
    tablewidget = new QTableWidget(stackwidget);
    tablewidget_1 = new QTableWidget(stackwidget);
    tablewidget_2 = new QTableWidget(stackwidget);
    tablewidget_3 = new QTableWidget(stackwidget);
    tablewidget_4 = new QTableWidget(stackwidget);

    mainwidget->resize(stackwidget->width(),stackwidget->height());
    tablewidget->resize(stackwidget->width(),stackwidget->height());
    tablewidget_1->resize(stackwidget->width(),stackwidget->height());
    tablewidget_2->resize(stackwidget->width(),stackwidget->height());
    tablewidget_3->resize(stackwidget->width(),stackwidget->height());
    tablewidget_4->resize(stackwidget->width(),stackwidget->height());

    stackwidget->addWidget(mainwidget);//主窗口 0
    stackwidget->addWidget(tablewidget);//加入文件及对应MD5显示table->index = 1
    stackwidget->addWidget(tablewidget_1);//加入匹配 2
    stackwidget->addWidget(tablewidget_2);//不匹配 3
    stackwidget->addWidget(tablewidget_3);//新的 4
    stackwidget->addWidget(tablewidget_4);//抓取的 5
    stackwidget->setCurrentIndex(0);

    /*mianwindow实现*/
    QFont font_main;
    font_main.setFamily("黑体");
    font_main.setPixelSize(30);
    mainwidget->setFont(font_main);
    mainwindowtitle = new QLabel(tr("   这是MD5校验工具。"),mainwidget);

    groupbox = new QGroupBox(tr("常规操作显示"),this);
    layout_2 = new QGridLayout(groupbox);
    //0,y
    searchPathLabel = new QLabel(tr("检查路径:"),groupbox);
    searchPathLineedit = new QLineEdit(groupbox);
    pushbutton = new QPushButton(tr("选择目录"),groupbox);
    testbtn = new QPushButton(tr("开始检验"),groupbox);
    //testbtn->setMinimumSize(pushbutton->width(),pushbutton->height());
    //testbtn->setMaximumSize(pushbutton->width(),pushbutton->height());
    pushbutton->setStyleSheet("QPushButton{background:rgb(54,54,54);"
                              "color:rgb(255,255,255);}"
                              "QPushButton:hover{background:rgb(238,44,44)}");
    testbtn->setStyleSheet("QPushButton{background:rgb(54,54,54);"
                           "color:rgb(255,255,255);}"
                           "QPushButton:hover{background:rgb(238,44,44)}");
    //1,y
    mismatchMd5Label = new QLabel(tr("不匹配的MD5:"),groupbox);
    mismatchMd5NumLineEdit = new QLineEdit(groupbox);
    matchMd5Label = new QLabel(tr("匹配的MD5序列:"),groupbox);
    matchMd5NumLineEdit = new QLineEdit(groupbox);
    //2,y
    newMd5Label = new QLabel(tr("新的MD5序列:"),groupbox);
    newMd5NumLineEdit = new QLineEdit(groupbox);
    sumMd5Label = new QLabel(tr("总的MD5序列数:"),groupbox);
    sumMd5NumLineEdit = new QLineEdit(groupbox);

    //输入框禁止输入
    mismatchMd5NumLineEdit->setReadOnly(true);
    matchMd5NumLineEdit->setReadOnly(true);
    newMd5NumLineEdit->setReadOnly(true);
    sumMd5NumLineEdit->setReadOnly(true);

    layout_2->addWidget(searchPathLabel,0,0,1,1);
    layout_2->addWidget(searchPathLineedit,0,1,1,1);
    layout_2->addWidget(pushbutton,0,2,1,1);

    layout_2->addWidget(testbtn,1,2,1,1);

    layout_2->addWidget(mismatchMd5Label,2,0,1,1);
    layout_2->addWidget(mismatchMd5NumLineEdit,2,1,1,1);
    layout_2->addWidget(matchMd5Label,2,2,1,1);
    layout_2->addWidget(matchMd5NumLineEdit,2,3,1,1);

    layout_2->addWidget(newMd5Label,3,0,1,1);
    layout_2->addWidget(newMd5NumLineEdit,3,1,1,1);
    layout_2->addWidget(sumMd5Label,3,2,1,1);
    layout_2->addWidget(sumMd5NumLineEdit,3,3,1,1);

    groupbox->setLayout(layout_2);


    layout = new QVBoxLayout(this);
    hlayout = new QHBoxLayout(this);
    //treewidget和stackedwidget水平布局
    treewidget = new QTreeWidget(this);
    hlayout->addWidget(treewidget,120);
    hlayout->setSpacing(1);
    hlayout->addWidget(stackwidget,width()-120);

    //进度框显示处理进度
    progressbar = new QProgressBar(this);
    progressbar->setMaximum(5);
    progressbar->setValue(provalue);
    //整体布局
    layout->addWidget(groupbox,140);
    layout->addWidget(progressbar);
    layout->addLayout(hlayout,height()-140);

    this->setLayout(layout);
    progressbar->hide();

    //treeWidget初始化
    QFont font_5;
    font_5.setPixelSize(14);
    treewidget->setFont(font_5);
    treewidget->setColumnCount(1);
    treewidget->setHeaderLabel(tr("MD5校验结果"));
    QTreeWidgetItem *file_md5 = new QTreeWidgetItem (treewidget);
    file_md5->setText(0,tr("MD5校验结果"));
    file_md5->setIcon(0,QIcon(":/new/prefix1/images/xinxi.png"));

    QTreeWidgetItem *item11=new QTreeWidgetItem(file_md5);
    item11->setText(0,"文件及对应MD5");
    item11->setIcon(0,QIcon(":/new/prefix1/images/wenjian.png"));

    QTreeWidgetItem *item12=new QTreeWidgetItem(file_md5);
    item12->setText(0,"抓取的MD5");
    item12->setIcon(0,QIcon(":/new/prefix1/images/wenjian.png"));

    QTreeWidgetItem *item13 = new QTreeWidgetItem(file_md5);
    item13->setText(0,"匹配的MD5");
    item13->setIcon(0,QIcon(":/new/prefix1/images/wenjian.png"));

    QTreeWidgetItem *item14 = new QTreeWidgetItem(file_md5);
    item14->setText(0,"不匹配的MD5");
    item14->setIcon(0,QIcon(":/new/prefix1/images/wenjian.png"));

    QTreeWidgetItem *item15 = new QTreeWidgetItem(file_md5);
    item15->setText(0,"新的MD5");
    item15->setIcon(0,QIcon(":/new/prefix1/images/wenjian.png"));


    //初始化表格
    initTable();

    connect(treewidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)), this,SLOT(checkself(QTreeWidgetItem* ,int)));

    connect(pushbutton,SIGNAL(clicked(bool)),this,SLOT(searchDir()));

    connect(testbtn,SIGNAL(clicked(bool)),this,SLOT(startTest()));

    connect(&m_thread,SIGNAL(finished()),this,SLOT(threadFinished()));

    connect(&m_thread,SIGNAL(sendData(QString,QString)),this,SLOT(rcevData_1(QString,QString)));

    connect(&m_thread,SIGNAL(sendTextData(QString,QString,QDateTime)),this,SLOT(recvData_5(QString,QString,QDateTime)));

    connect(&m_thread,SIGNAL(sendMismatchMd5(QString,QString)),this,SLOT(recvData_3(QString,QString)));

    connect(&m_thread,SIGNAL(sendNewMd5(QString,QString)),this,SLOT(recvData_4(QString,QString)));

    connect(&m_thread,SIGNAL(sendMatchMd5(QString,QString)),this,SLOT(recvData_2(QString,QString)));
}

md5Test::~md5Test()
{
    delete tablewidget;
    delete pushbutton;
    delete groupbox;
    delete searchPathLineedit;
    delete mismatchMd5NumLineEdit;
    delete matchMd5NumLineEdit;
    delete newMd5NumLineEdit;
    delete sumMd5NumLineEdit;
    delete searchPathLabel;
    delete mismatchMd5Label;
    delete matchMd5Label;
    delete newMd5Label;
    delete sumMd5Label;
    delete layout;
    delete layout_2;
    delete treewidget;
    delete hlayout;
    delete tablewidget_1;
    delete tablewidget_2;
    delete tablewidget_3;
    delete tablewidget_4;
    delete stackwidget;
    delete mainwidget;
    delete mainwindowtitle;
    delete testbtn;
}

void md5Test::threadFinished()
{
    progressbar->setValue(5);
    qDebug()<<"Thread end.";
    m_thread.quit();
    setCursor(Qt::OpenHandCursor);
    mismatchMd5NumLineEdit->setText(QString::number(tablewidget_2->rowCount(),10));
    matchMd5NumLineEdit->setText((QString::number(tablewidget_1->rowCount(),10)));
    newMd5NumLineEdit->setText(QString::number(tablewidget_3->rowCount(),10));
    sumMd5NumLineEdit->setText(QString::number(tablewidget->rowCount(),10));
}

//1
void md5Test::rcevData_1(QString filepath,QString md5)
{
    progressbar->setValue(1);
    row++;
    tablewidget->setRowCount(row);
    //将读取的数据显示在表格
    tablewidget->setItem(row-1,0,new QTableWidgetItem(filepath));
    tablewidget->setItem(row-1,1,new QTableWidgetItem(md5));
}

//文件中的2
void md5Test::recvData_5(QString filepath,QString md5,QDateTime time)
{
    progressbar->setValue(1);
    row_4++;
    tablewidget_4->setRowCount(row_4);
    //将读取的数据显示在表格
    tablewidget_4->setItem(row_4-1,0,new QTableWidgetItem(filepath));
    tablewidget_4->setItem(row_4-1,1,new QTableWidgetItem(md5));
    tablewidget_4->setItem(row_4-1,2,new QTableWidgetItem(time.toString()));
}

//接受不匹配的MD5   3
void md5Test::recvData_3(QString filepath,QString md5)
{
    progressbar->setValue(2);
    row_2++;
    tablewidget_2->setRowCount(row_2);
    //将数据显示在表格、
    tablewidget_2->setItem(row_2-1,0,new QTableWidgetItem(filepath));
    tablewidget_2->setItem(row_2-1,1,new QTableWidgetItem(md5));
}

//接受新的MD5  5
void md5Test::recvData_4(QString filepath,QString md5)
{
    progressbar->setValue(4);
    row_3++;
    tablewidget_3->setRowCount(row_3);
    tablewidget_3->setItem(row_3-1,0,new QTableWidgetItem(filepath));
    tablewidget_3->setItem(row_3-1,1,new QTableWidgetItem(md5));
}

//接收匹配MD5  4
void md5Test::recvData_2(QString filepath,QString md5)
{
    progressbar->setValue(3);
    row_1++;
    tablewidget_1->setRowCount(row_1);
    tablewidget_1->setItem(row_1-1,0,new QTableWidgetItem(filepath));
    tablewidget_1->setItem(row_1-1,1,new QTableWidgetItem(md5));
}

void md5Test::searchDir()
{
    //选择目录
    //打开文件系统
    QString dir = QFileDialog::getExistingDirectory(this,tr("选择路径"),"/home");
    searchPathLineedit->setText(dir);
    setWindowTitle(dir);

    QFile file("mydir.gaoke");
    if(file.open(QIODevice::WriteOnly))
    {
        QTextStream in(&file);
        in<<dir;
    }
    file.close();
}

void md5Test::startTest()
{
    row = 0;
    row_1 = 0;
    row_2 = 0;
    row_3 = 0;
    row_4 = 0;
    QFileInfo file_info(searchPathLineedit->text());
    if(file_info.exists())
    {
        clearTableRow();
        initTable();
        m_thread.start();
        setCursor(Qt::WaitCursor);
        progressbar->show();
    }
    else {
        //检查目录不存在
        QErrorMessage mes(this);
        mes.setWindowTitle(tr("错误信息"));
        mes.showMessage(tr("请输入正确的检查目录!!"));
        mes.exec();
    }
}

//treewidget 点击事件slot
void md5Test::checkself(QTreeWidgetItem *item,int flag)
{
    //qDebug()<<"treewidget item = "<<flag<<item->text(flag);
    QString item_data = item->text(flag);
    if(item_data == "文件及对应MD5")
    {
        stackwidget->setCurrentIndex(1);
    }
    if(item_data == "抓取的MD5")
    {
        stackwidget->setCurrentIndex(5);
    }
    if(item_data == "匹配的MD5")
    {
        stackwidget->setCurrentIndex(2);
    }
    if(item_data == "不匹配的MD5")
    {
        stackwidget->setCurrentIndex(3);
    }
    if(item_data == "新的MD5")
    {
        stackwidget->setCurrentIndex(4);
    }
}

//文件及对应md5
void md5Test::viewFileMd5()
{
    QFont font;
    font.setBold(true);
    font.setPixelSize(14);
    tablewidget->setFont(font);
    //设置tablewidget风格
    tablewidget->setColumnCount(2);//设置列数
    QStringList listName;
    listName<<"文件及路径"<<"MD5序列号";
    tablewidget->setHorizontalHeaderLabels(listName);
    tablewidget->horizontalHeader()->setStretchLastSection(true);
    tablewidget->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止编辑
    tablewidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//平均分配列宽
    tablewidget->setStyleSheet("selection-background-color:rgb(0,0,255);");//单元格背景色
}

//显示text中抓取的MD5表格设置
void md5Test::viewTextMd5()
{
    QFont font;
    font.setBold(true);
    font.setPixelSize(14);
    tablewidget_4->setFont(font);
    //设置tablewidget风格
    tablewidget_4->setColumnCount(3);//设置列数
    QStringList listName;
    listName<<"文件及路径"<<"MD5序列号"<<"文件创建时间";
    tablewidget_4->setHorizontalHeaderLabels(listName);
    tablewidget_4->horizontalHeader()->setStretchLastSection(true);
    tablewidget_4->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止编辑
    tablewidget_4->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//平均分配列宽
    tablewidget_4->setStyleSheet("selection-background-color:rgb(0,0,255);");//单元格背景色
}

//显示不匹配MD5表格
void md5Test::viewMismatchMd5()
{
    QFont font;
    font.setBold(true);
    font.setPixelSize(14);
    tablewidget_2->setFont(font);
    //设置tablewidget风格
    tablewidget_2->setColumnCount(2);//设置列数
    QStringList listName;
    listName<<"文件及路径"<<"不匹配的MD5序列号";
    tablewidget_2->setHorizontalHeaderLabels(listName);
    tablewidget_2->horizontalHeader()->setStretchLastSection(true);
    tablewidget_2->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止编辑
    tablewidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//平均分配列宽
    tablewidget_2->setStyleSheet("selection-background-color:rgb(0,0,255);");//单元格背景色
}

//显示新的MD5表格
void md5Test::viewNewMd5()
{
    QFont font;
    font.setBold(true);
    font.setPixelSize(14);
    tablewidget_3->setFont(font);
    //设置tablewidget风格
    tablewidget_3->setColumnCount(2);//设置列数
    QStringList listName;
    listName<<"文件及路径"<<"计算文件在TXT中没有的MD5序列号";
    tablewidget_3->setHorizontalHeaderLabels(listName);
    tablewidget_3->horizontalHeader()->setStretchLastSection(true);
    tablewidget_3->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止编辑
    tablewidget_3->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//平均分配列宽
    tablewidget_3->setStyleSheet("selection-background-color:rgb(0,0,255);");//单元格背景色
}

//显示匹配MD5表格
void md5Test::viewMatchMd5()
{
    QFont font;
    font.setBold(true);
    font.setPixelSize(14);
    tablewidget_1->setFont(font);
    //设置tablewidget风格
    tablewidget_1->setColumnCount(2);//设置列数
    QStringList listName;
    listName<<"文件及路径"<<"匹配的MD5序列号";
    tablewidget_1->setHorizontalHeaderLabels(listName);
    tablewidget_1->horizontalHeader()->setStretchLastSection(true);
    tablewidget_1->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止编辑
    tablewidget_1->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//平均分配列宽
    tablewidget_1->setStyleSheet("selection-background-color:rgb(0,0,255);");//单元格背景色
}

//初始化表格
void md5Test::initTable()
{
    viewFileMd5();//文件及对应md5
    viewTextMd5();//显示text中抓取的MD5表格设置
    viewMismatchMd5();//显示不匹配的MD5序列
    viewNewMd5();//显示新的MD5
    viewMatchMd5();//显示匹配MD5
}

void md5Test::clearTableRow()
{

    for(int i = 0; i < tablewidget->rowCount();i++)
    {
        tablewidget->removeRow(i);
    }

    for(int i = 0; i < tablewidget_1->rowCount();i++)
    {
        tablewidget_1->removeRow(i);
    }
    for(int i = 0; i < tablewidget_2->rowCount();i++)
    {
        tablewidget_2->removeRow(i);
    }

    for(int i = 0; i < tablewidget_3->rowCount();i++)
    {
        tablewidget_3->removeRow(i);
    }
    for(int i = 0; i < tablewidget_4->rowCount();i++)
    {
        tablewidget_4->removeRow(i);
    }

    tablewidget->clear();
    tablewidget_1->clear();
    tablewidget_2->clear();
    tablewidget_3->clear();
    tablewidget_4->clear();
}

接下来是在该进程下开的一个新线程,用于实现业务方面的处理。

md5Thread.h

#ifndef MD5THREAD_H
#define MD5THREAD_H

#include <QObject>
#include <QWidget>
#include <QThread>
#include <qfileinfo.h>
#include <QDirIterator>
#include <QDateTime>
class md5Thread : public QThread
{
    Q_OBJECT
private slots:
signals:
    void sendMes(QString);
    void sendData(QString,QString);
    void sendTextData(QString,QString,QDateTime);
    void sendMismatchMd5(QString,QString);
    void sendNewMd5(QString,QString);
    void sendMatchMd5(QString,QString);
public:
    md5Thread();
    void run();
private:
    QString dir;
    QStringList filemd5list,md5list;

    bool mismatch;

};

#endif // MD5THREAD_H

md5Thread.cpp

#include "md5thread.h"
#include <qdebug.h>
#include <QFileDialog>
#include <QCryptographicHash>
md5Thread::md5Thread()
{
    mismatch = true;
}

void md5Thread::run()
{
    qDebug()<<"MD5线程开启。";
    QFile file("mydir.gaoke");
    if(file.open(QIODevice::ReadOnly))
    {
        QTextStream get(&file);
        dir = get.readAll();
    }
    file.close();

    //定义迭代器并设置过滤器
    QDirIterator dir_iterator(dir,
                              QDir::Files | QDir::NoSymLinks,
                              QDirIterator::Subdirectories);
    while(dir_iterator.hasNext())
    {
        dir_iterator.next();
        QFileInfo file_info = dir_iterator.fileInfo();
        QString filepath = file_info.filePath();
        QDateTime filetime = file_info.birthTime();//文件创建时间
        //计算文件对应md5
        if(filepath.endsWith(".txt",Qt::CaseInsensitive) != true)
        {
            QFile file(filepath);//打开用户选择的文件

            //读取文件计算文件md5
            if(file.open(QIODevice::ReadOnly))//以只读的方式打开文件并判断文件是否被成功打开
            {
                QByteArray array = file.readAll();//读取文件的所有内容
                //计算md5值,传入需要被计算的数据,传入计算的类型
                QByteArray md5 = QCryptographicHash::hash(array,QCryptographicHash::Md5);
                //sendMes(filepath);
                //将文件对应的MD5存在list中
                QString value = filepath +"&"+QString(md5.toHex().toUpper());
                filemd5list.append(value);
                emit sendData(filepath,QString(md5.toHex()).toUpper());//
            }
        }
        //抓取text文件中的md5
        if(filepath.endsWith(".txt",Qt::CaseInsensitive) == true)
        {
            QFile textfile(filepath);
            if(textfile.open(QIODevice::ReadOnly|QIODevice::Text))
            {
                QTextStream get(&textfile);
                while(!get.atEnd())
                {
                    QString data = get.readLine();
                    if(data.contains("md5",Qt::CaseInsensitive))
                    {
                        //获取md5串;section获取以冒号分隔第二部分串;simplified去掉首尾空格;toUpper转换为大写
                        QString getmd5 = data.section(":",1,1).simplified().toUpper();
                        md5list.append(getmd5);
                        if(getmd5 == nullptr)
                        {
                            emit sendTextData(filepath,data,filetime);//
                        }
                        if(getmd5!=nullptr)
                        {
                            emit sendTextData(filepath,getmd5,filetime);//
                        }

                        //获取不匹配的MD5
                        QList<QString>::Iterator itbegin = filemd5list.begin(),itend = filemd5list.end();
                        for(;itbegin!=itend;itbegin++)
                        {
                            if(getmd5 == itbegin->section("&",1,1))
                            {
                                mismatch = false;
                                //发送匹配的MD5序列
                                emit sendMatchMd5(filepath,getmd5);//
                            }
                        }
                        //mismatch = true没有对应MD5(表示不匹配)发送该Info
                        if(mismatch == true && getmd5.isEmpty() == false)
                        {
                            emit sendMismatchMd5(filepath,getmd5);//
                        }
                    }
                    mismatch = true;
                }
            }
        }
    }
    //获取计算文件的MD5在txt中没有匹配项(新的)
    QList<QString>::Iterator itb = md5list.begin(),ite = md5list.end();
    QList<QString>::Iterator itb_1 = filemd5list.begin(),ite_1 = filemd5list.end();
    bool exist = false;
    for(;itb_1!=ite_1;itb_1++)
    {
        for(;itb!=ite;itb++)
        {
            if(itb_1->section("&",1,1) == *itb)
            {
                exist = true;
            }
        }
        if(exist == false)
            emit sendNewMd5(itb_1->section("&",0,0),itb_1->section("&",1,1));
        exist = false;
    }
    filemd5list.clear();
    md5list.clear();
}

入口函数main.cpp的实现:

#include "md5test.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    md5Test w;
    w.show();

    return a.exec();
}

此外在线程间的通信方式通过信号槽进行实现



版权声明:本文为weixin_41151210原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。