From 3c21e27f450a1446751fa26ef84d4019a0b4b8ff Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 10 Apr 2020 10:45:19 +0200 Subject: [PATCH] gui/SearchWindow: Use QFutureWatcher instead of QThread. By removing the call to QThread::create() that requires version >= 5.11 we can have a lower version as requirement. This is useful because a lot of systems don't ship with 5.11 as default. --- gui/CMakeLists.txt | 4 ++-- gui/SearchWindow.cpp | 35 +++++++++-------------------------- gui/SearchWindow.h | 6 ++---- 3 files changed, 13 insertions(+), 32 deletions(-) diff --git a/gui/CMakeLists.txt b/gui/CMakeLists.txt index 94b0226..8c330ed 100644 --- a/gui/CMakeLists.txt +++ b/gui/CMakeLists.txt @@ -20,8 +20,8 @@ set( PROGRAM_SRC add_executable( ${PROGRAM_EXE} ${PROGRAM_SRC} ) # Libraries -find_package( Qt5 5.11 COMPONENTS Core Widgets REQUIRED ) -target_link_libraries( ${PROGRAM_EXE} Qt5::Core Qt5::Widgets common ) +find_package( Qt5 5.11 COMPONENTS Concurrent Core Widgets REQUIRED ) +target_link_libraries( ${PROGRAM_EXE} Qt5::Concurrent Qt5::Core Qt5::Widgets common ) # -------------------------------- # Install diff --git a/gui/SearchWindow.cpp b/gui/SearchWindow.cpp index fae2321..e34f556 100644 --- a/gui/SearchWindow.cpp +++ b/gui/SearchWindow.cpp @@ -24,7 +24,8 @@ #include #include #include -#include +#include +#include #include #include #include @@ -32,7 +33,6 @@ SearchWindow::SearchWindow(QWidget *parent, Qt::WindowFlags flags) : QWidget (parent, flags), -m_worker (NULL), m_status ("status"), m_leet_cb ("L33t"), m_btn_exec ("Search"), @@ -86,21 +86,16 @@ m_btn_clear ("Clear") m_txt_search.setFocus(); } -SearchWindow::~SearchWindow() -{ - // Make sure worker thread exits. - if (m_worker) { - m_worker->quit(); - m_worker->wait(); - } -} - void SearchWindow::initSignals() { // Buttons connect(&m_btn_exec, SIGNAL(released()), this, SLOT(search())); connect(&m_btn_clear, SIGNAL(released()), &m_output, SLOT(clear())); + // Worker Thread + connect(&m_worker, SIGNAL(started()), this, SLOT(searchStarted())); + connect(&m_worker, SIGNAL(finished()), this, SLOT(searchFinished())); + connect(this, SIGNAL(addOutput(QString)), this, SLOT(output(QString))); } @@ -137,7 +132,7 @@ void SearchWindow::onResult(const struct libeosio::ec_keypair* key, const struct void SearchWindow::search() { - if (m_worker && m_worker->isRunning()) { + if (m_worker.isRunning()) { return; } @@ -162,16 +157,10 @@ void SearchWindow::search() m_ksearch.addList(list); m_ksearch.setThreadCount(m_num_threads.value()); - // Create search thread - m_worker = QThread::create([this] { - m_ksearch.find(m_num_results.value()); - }); - - connect(m_worker, SIGNAL(started()), this, SLOT(searchStarted())); - connect(m_worker, SIGNAL(finished()), this, SLOT(searchFinished())); + QFuture future = QtConcurrent::run(m_ksearch, &eoskeygen::KeySearch::find, m_num_results.value()); + m_worker.setFuture(future); m_status.setText("Searching for: " + QString::fromStdString(eoskeygen::strlist::join(list, ", "))); - m_worker->start(); } void SearchWindow::output(const std::string& html) @@ -206,10 +195,4 @@ void SearchWindow::searchFinished() m_btn_clear.setEnabled(true); m_num_threads.setEnabled(true); m_num_results.setEnabled(true); - - // We are done with the worker pointer. - if (m_worker) { - delete m_worker; - m_worker = NULL; - } } diff --git a/gui/SearchWindow.h b/gui/SearchWindow.h index 2959c02..0bc6a65 100644 --- a/gui/SearchWindow.h +++ b/gui/SearchWindow.h @@ -31,18 +31,16 @@ #include #include #include +#include #include #include #include -class QThread; - class SearchWindow : public QWidget, public eoskeygen::IKeySearchResult { Q_OBJECT public: explicit SearchWindow(QWidget *parent = 0, Qt::WindowFlags flags = Qt::WindowFlags()); - virtual ~SearchWindow(); void onResult(const struct libeosio::ec_keypair* key, const struct eoskeygen::KeySearch::result& result); @@ -70,7 +68,7 @@ signals: private: // Search worker thread. - QThread* m_worker; + QFutureWatcher m_worker; eoskeygen::KeySearch m_ksearch;