Skip to content

Commit 0ae22fd

Browse files
committed
Offload triage view hash calculations from the main thread.
1 parent 09ab887 commit 0ae22fd

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

examples/triage/fileinfo.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#include <QApplication>
77
#include <QToolTip>
88
#include <QPainter>
9+
#include <QtConcurrent/QtConcurrent>
10+
#include <QFuture>
11+
#include <QFutureWatcher>
912

1013
void FileInfoWidget::addCopyableField(const QString& name, const QVariant& value)
1114
{
@@ -35,12 +38,31 @@ void FileInfoWidget::addHashField(
3538
auto& [row, column] = this->m_fieldPosition;
3639

3740
const auto hashFieldColor = getThemeColor(AlphanumericHighlightColor);
38-
const auto crypto = QCryptographicHash::hash(data, algorithm);
39-
const auto hashLabel = new CopyableLabel(crypto.toHex(), hashFieldColor);
41+
auto hashLabel = new CopyableLabel("Calculating...", hashFieldColor);
4042
hashLabel->setFont(getMonospaceFont(this));
4143

4244
this->m_layout->addWidget(new QLabel(hashName), row, column);
4345
this->m_layout->addWidget(hashLabel, row++, column + 1);
46+
47+
// Process the hash calculations in a separate thread and update the label when done
48+
QPointer<QFutureWatcher<QByteArray>> watcher = new QFutureWatcher<QByteArray>(this);
49+
connect(watcher, &QFutureWatcher<QByteArray>::finished, this, [watcher, hashLabel]() {
50+
if (watcher)
51+
{
52+
hashLabel->setText(watcher->result().toHex());
53+
watcher->deleteLater();
54+
}
55+
});
56+
QFuture<QByteArray> future = QtConcurrent::run([data, algorithm]() {
57+
return QCryptographicHash::hash(data, algorithm);
58+
});
59+
watcher->setFuture(future);
60+
connect(this, &QObject::destroyed, this, [watcher]() {
61+
if (watcher && watcher->isRunning()) {
62+
watcher->cancel();
63+
watcher->waitForFinished();
64+
}
65+
});
4466
}
4567

4668
FileInfoWidget::FileInfoWidget(QWidget* parent, BinaryViewRef bv)

0 commit comments

Comments
 (0)