From e39c9cff1a626ee038a541258d6e271ce63969a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Sat, 12 Oct 2019 22:26:47 +0100 Subject: gui: Fix start timer from non QThread Github-Pull: #17120 Rebased-From: a8f5026d6d992fd8d72908c848c5028f0f9a8cd1 Tree-SHA512: 5f8cbfd5044d2c0df7dbfc54e7d94f93da96d0adecd23007e82acc38bc3c7984a685c9596cbd449f559cf42db9e1b99612765328004c5a45655a8e375482a8a8 --- src/qt/walletcontroller.cpp | 6 ++++++ src/qt/walletmodel.cpp | 13 ++++++++----- src/qt/walletmodel.h | 5 +++-- 3 files changed, 17 insertions(+), 7 deletions(-) (limited to 'src/qt') diff --git a/src/qt/walletcontroller.cpp b/src/qt/walletcontroller.cpp index fa6f9f3f16..a8de5392b4 100644 --- a/src/qt/walletcontroller.cpp +++ b/src/qt/walletcontroller.cpp @@ -108,6 +108,12 @@ WalletModel* WalletController::getOrCreateWallet(std::unique_ptrsetParent(this); m_wallets.push_back(wallet_model); + // WalletModel::startPollBalance needs to be called in a thread managed by + // Qt because of startTimer. Considering the current thread can be a RPC + // thread, better delegate the calling to Qt with Qt::AutoConnection. + const bool called = QMetaObject::invokeMethod(wallet_model, "startPollBalance"); + assert(called); + connect(wallet_model, &WalletModel::unload, [this, wallet_model] { // Defer removeAndDeleteWallet when no modal widget is active. // TODO: remove this workaround by removing usage of QDiallog::exec. diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index 49a13330ec..05ab805bfd 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -44,11 +44,6 @@ WalletModel::WalletModel(std::unique_ptr wallet, interfaces: transactionTableModel = new TransactionTableModel(platformStyle, this); recentRequestsTableModel = new RecentRequestsTableModel(this); - // This timer will be fired repeatedly to update the balance - pollTimer = new QTimer(this); - connect(pollTimer, &QTimer::timeout, this, &WalletModel::pollBalanceChanged); - pollTimer->start(MODEL_UPDATE_DELAY); - subscribeToCoreSignals(); } @@ -57,6 +52,14 @@ WalletModel::~WalletModel() unsubscribeFromCoreSignals(); } +void WalletModel::startPollBalance() +{ + // This timer will be fired repeatedly to update the balance + QTimer* timer = new QTimer(this); + connect(timer, &QTimer::timeout, this, &WalletModel::pollBalanceChanged); + timer->start(MODEL_UPDATE_DELAY); +} + void WalletModel::updateStatus() { EncryptionStatus newEncryptionStatus = getEncryptionStatus(); diff --git a/src/qt/walletmodel.h b/src/qt/walletmodel.h index 54428aec08..a1bdba8222 100644 --- a/src/qt/walletmodel.h +++ b/src/qt/walletmodel.h @@ -255,8 +255,6 @@ private: EncryptionStatus cachedEncryptionStatus; int cachedNumBlocks; - QTimer *pollTimer; - void subscribeToCoreSignals(); void unsubscribeFromCoreSignals(); void checkBalanceChanged(const interfaces::WalletBalances& new_balances); @@ -292,6 +290,9 @@ Q_SIGNALS: void canGetAddressesChanged(); public Q_SLOTS: + /* Starts a timer to periodically update the balance */ + void startPollBalance(); + /* Wallet status might have changed */ void updateStatus(); /* New transaction, or transaction changed status */ -- cgit v1.2.3