From 0ae22fd471405754633b88aaa874d412abefabdf Mon Sep 17 00:00:00 2001 From: Brian Potchik Date: Thu, 16 Jan 2025 16:25:13 -0500 Subject: Offload triage view hash calculations from the main thread. --- examples/triage/fileinfo.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/examples/triage/fileinfo.cpp b/examples/triage/fileinfo.cpp index dbecaddc..ca96a9dd 100644 --- a/examples/triage/fileinfo.cpp +++ b/examples/triage/fileinfo.cpp @@ -6,6 +6,9 @@ #include #include #include +#include +#include +#include void FileInfoWidget::addCopyableField(const QString& name, const QVariant& value) { @@ -35,12 +38,31 @@ void FileInfoWidget::addHashField( auto& [row, column] = this->m_fieldPosition; const auto hashFieldColor = getThemeColor(AlphanumericHighlightColor); - const auto crypto = QCryptographicHash::hash(data, algorithm); - const auto hashLabel = new CopyableLabel(crypto.toHex(), hashFieldColor); + auto hashLabel = new CopyableLabel("Calculating...", hashFieldColor); hashLabel->setFont(getMonospaceFont(this)); this->m_layout->addWidget(new QLabel(hashName), row, column); this->m_layout->addWidget(hashLabel, row++, column + 1); + + // Process the hash calculations in a separate thread and update the label when done + QPointer> watcher = new QFutureWatcher(this); + connect(watcher, &QFutureWatcher::finished, this, [watcher, hashLabel]() { + if (watcher) + { + hashLabel->setText(watcher->result().toHex()); + watcher->deleteLater(); + } + }); + QFuture future = QtConcurrent::run([data, algorithm]() { + return QCryptographicHash::hash(data, algorithm); + }); + watcher->setFuture(future); + connect(this, &QObject::destroyed, this, [watcher]() { + if (watcher && watcher->isRunning()) { + watcher->cancel(); + watcher->waitForFinished(); + } + }); } FileInfoWidget::FileInfoWidget(QWidget* parent, BinaryViewRef bv) -- cgit v1.3.1