Qt实现的MD5文件计算小工具

想实现自动更新工具然后参考了一下 https://blog.csdn.net/hulinhulin/article/details/46839107觉得有些东西需要扩展

在这自己随便写了一个MD5的小工具方便之后文件下载验证

Dialog.h头文件中

class Dialog : public QDialog
{ 
    Q_OBJECT
public:  
    explicit Dialog(QWidget *parent = nullptr);  
    ~Dialog();
    void initForm();
    void showFileCalcValue();
private slots:  
    void on_pushButton_clicked();   
    void on_comboBox_currentIndexChanged(int index);
protected:   
    void dragEnterEvent(QDragEnterEvent *e);   
    void dropEvent(QDropEvent *e);
private:  
    Ui::Dialog *ui;
};

 cpp文件中

#include "dialog.h"
#include "ui_dialog.h"
#include <QFileDialog>
#include <QDebug>
#include <QCryptographicHash>
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    initForm();
    this->setWindowTitle("文件MD5计算工具 by CZP");
    this->setWindowFlag(Qt::WindowType::WindowStaysOnTopHint);
}
Dialog::~Dialog()
{
    delete ui;
}
void Dialog::initForm()
{
    ui->comboBox->addItem("Md5",QCryptographicHash::Md5);
    ui->comboBox->addItem("Md4",QCryptographicHash::Md4);
    ui->comboBox->addItem("Sha1",QCryptographicHash::Sha1);
    ui->comboBox->addItem("Sha224",QCryptographicHash::Sha224);
    ui->comboBox->addItem("Sha256",QCryptographicHash::Sha256);
    ui->comboBox->addItem("Sha384",QCryptographicHash::Sha384);
    ui->comboBox->addItem("Sha512",QCryptographicHash::Sha512);
    ui->comboBox->addItem("Sha3_224",QCryptographicHash::Sha3_224);
    ui->comboBox->addItem("Sha3_256",QCryptographicHash::Sha3_256);
    ui->comboBox->addItem("Sha3_384",QCryptographicHash::Sha3_384);
    ui->comboBox->addItem("Sha3_512",QCryptographicHash::Sha3_512);
    ui->comboBox->addItem("Keccak_224",QCryptographicHash::Keccak_224);
    ui->comboBox->addItem("Keccak_256",QCryptographicHash::Keccak_256);
    ui->comboBox->addItem("Keccak_384",QCryptographicHash::Keccak_384);
    ui->comboBox->addItem("Keccak_512",QCryptographicHash::Keccak_512);
}
void Dialog::on_pushButton_clicked()
{
    QString fileName=QFileDialog::getOpenFileName(this);
    QFileInfo fileInfo(fileName);
    ui->label->setText(fileInfo.fileName());
    ui->label->setToolTip(fileName);
    showFileCalcValue();
}
void Dialog::on_comboBox_currentIndexChanged(int index)
{
    showFileCalcValue();
}
#include <QDragEnterEvent>
#include <QMimeData>

void Dialog::dragEnterEvent(QDragEnterEvent *e)
{
    //if(e->mimeData()->hasFormat("text/uri-list")) //只能打开文本文件
    e->acceptProposedAction(); //可以在这个窗口部件上拖放对象
}
void Dialog::dropEvent(QDropEvent *e)
{
    QList<QUrl> urls = e->mimeData()->urls();
    if(urls.isEmpty())
        return ;

    QString fileName = urls.first().toLocalFile();

    foreach (QUrl u, urls) {
        qDebug()<<u.toString();
    }
    qDebug()<< urls.size();

    if(fileName.isEmpty())
        return;


    QFileInfo fileInfo(fileName);
    ui->label->setText(fileInfo.fileName());
    ui->label->setToolTip(fileName);
    showFileCalcValue();
}
void Dialog::showFileCalcValue()
{
    const QString& fileName=ui->label->toolTip();
    if(fileName=="")return;
    QFile theFile(fileName);
    if(!theFile.open(QIODevice::ReadOnly))return;
    int method=ui->comboBox->currentData().toInt();
    QByteArray ba = QCryptographicHash::hash(theFile.readAll(),static_cast<QCryptographicHash::Algorithm>(method));
    theFile.close();
    qDebug()<<ba.toHex().constData();
    ui->textBrowser->clear();
    ui->textBrowser->insertPlainText(ba.toHex().constData());
}

 这样就完成了

成品效果图

猜你喜欢

转载自blog.csdn.net/qq_35665222/article/details/86704599