diff options
| author | Brian Potchik <brian@vector35.com> | 2025-01-16 16:25:13 -0500 |
|---|---|---|
| committer | Brian Potchik <brian@vector35.com> | 2025-01-16 16:25:13 -0500 |
| commit | 0ae22fd471405754633b88aaa874d412abefabdf (patch) | |
| tree | b290b1d33898355ebeb0ed2e96b9885844bc1302 /examples | |
| parent | 09ab8876570d0da38e9a8c9d9e0c3c9076846ac1 (diff) | |
Offload triage view hash calculations from the main thread.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/triage/fileinfo.cpp | 26 |
1 files 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 <QApplication> #include <QToolTip> #include <QPainter> +#include <QtConcurrent/QtConcurrent> +#include <QFuture> +#include <QFutureWatcher> 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<QFutureWatcher<QByteArray>> watcher = new QFutureWatcher<QByteArray>(this); + connect(watcher, &QFutureWatcher<QByteArray>::finished, this, [watcher, hashLabel]() { + if (watcher) + { + hashLabel->setText(watcher->result().toHex()); + watcher->deleteLater(); + } + }); + QFuture<QByteArray> 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) |
