【发布时间】:2021-02-06 21:07:15
【问题描述】:
嗨,我有一个类用于在 Qt 中生成某个文件的 MD5,(我使用元组从中返回多个值),我想在其他线程上运行它,因为生成所有文件 MD5 可能需要一些时间,而且它冻结图形界面
我决定使用 QtConcurrentRun 在其他线程上运行它,但到目前为止我对如何获取所有元组返回值没有任何想法
这是我的代码
HashGen.h
#pragma once
#include "stdafx.h"
class HashGen : public QObject
{
Q_OBJECT
public:
HashGen(QObject *parent = nullptr);
private:
QString Md5_gen(QString const& fname);
public slots:
std::tuple<int, int, int> check_sequential();
};
HashGen.cpp
#include "stdafx.h"
#include "HashGen.h"
HashGen::HashGen(QObject *parent)
: QObject(parent)
{
}
QString HashGen::Md5_gen(QString const& fname)
{
//Generate MD5 of giving file name
}
std::tuple<int, int, int> HashGen::check_sequential() {
QString file1[2] = { "8c0b1e6492078bdc113faae3d34fa5c5", "" }; // empty "" fill with other MD5 hash later
QString file2[4] = { "0547f42982dd9edb7b47931d00efff15", "", "", "" };
QString file3[2] = { "57f08e690e2655749291b2da4be8b021", "" };
QString file1_H = Md5_gen("/proj/file.zip");
QString file2_H = Md5_gen("/proj/file2.zip");
QString file3_H = Md5_gen("/proj/file3.zip");
int file1_status = 0;
int file2_status = 0;
int file3_status = 0;
for (int i = 0; i < 2; i++)
{
if (file1[i] != "nofile")
{
if (file1[i] == file1_H)
{
file1_status = i;
break;
}
else { file1_status = 422; } // Just a random number mean file doesn't match any MD5
}
else
{
file1_status = 404; // Just a random number mean file doesn't exist
break;
}
}
for (int i = 0; i < 4; i++)
{
if (file2[i] != "nofile")
{
if (file2[i] == file2_H)
{
file2_status = i;
break;
}
else { file2_status = 422; }
}
else
{
file2_status = 404;
break;
}
}
for (int i = 0; i < 2; i++)
{
if (file3[i] != "nofile")
{
if (file3[i] == file3_H)
{
file3_status = i;
break;
}
else { file3_status = 422; }
}
else
{
file3_status = 404;
break;
}
}
return { file1_status, file2_status, file3_status}; // Return all file status
mainwindow.cpp
void mainwindow::on_pushButton_clicked()
{
QFuture<std::tuple<int, int, int>> Hash = QtConcurrent::run(Gen, &HashGen::check_sequential);
QFutureWatcher<std::tuple<int, int, int>>* watcher = new QFutureWatcher<std::tuple<int, int, int>>;
connect(watcher, &QFutureWatcher<std::tuple<int, int, int>>::finished, this, &MafiaDEDLFox::AfterHash);
watcher->setFuture(Hash);
}
另一个问题是我需要使用 QFuturewatcher 来监视 QFuture,但我不知道在哪里声明它的最佳位置(所以当函数超出范围时它不会删除) 对不起,如果我不能正确解释我的问题,但我希望有人帮助我,谢谢
【问题讨论】: