Skip to content

Commit 9bd109b

Browse files
committed
Merge bitcoin#17120: gui: Fix start timer from non QThread
a8f5026 gui: Fix start timer from non QThread (João Barbosa) Pull request description: Fixes bitcoin#16296. ACKs for top commit: laanwj: code review ACK a8f5026 Tree-SHA512: d7b05ac88e188de16cbbe80cb2f773b7976ee07ee876ac94a93f9351856c4f3a9d66a531d3f3748d2dccff8c8d77d9d8227433069ed5909c32be2efeaa32f655
2 parents 3f87574 + a8f5026 commit 9bd109b

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/qt/walletcontroller.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wal
109109
wallet_model->setParent(this);
110110
m_wallets.push_back(wallet_model);
111111

112+
// WalletModel::startPollBalance needs to be called in a thread managed by
113+
// Qt because of startTimer. Considering the current thread can be a RPC
114+
// thread, better delegate the calling to Qt with Qt::AutoConnection.
115+
const bool called = QMetaObject::invokeMethod(wallet_model, "startPollBalance");
116+
assert(called);
117+
112118
connect(wallet_model, &WalletModel::unload, [this, wallet_model] {
113119
// Defer removeAndDeleteWallet when no modal widget is active.
114120
// TODO: remove this workaround by removing usage of QDiallog::exec.

src/qt/walletmodel.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ WalletModel::WalletModel(std::unique_ptr<interfaces::Wallet> wallet, interfaces:
4444
transactionTableModel = new TransactionTableModel(platformStyle, this);
4545
recentRequestsTableModel = new RecentRequestsTableModel(this);
4646

47-
// This timer will be fired repeatedly to update the balance
48-
pollTimer = new QTimer(this);
49-
connect(pollTimer, &QTimer::timeout, this, &WalletModel::pollBalanceChanged);
50-
pollTimer->start(MODEL_UPDATE_DELAY);
51-
5247
subscribeToCoreSignals();
5348
}
5449

@@ -57,6 +52,14 @@ WalletModel::~WalletModel()
5752
unsubscribeFromCoreSignals();
5853
}
5954

55+
void WalletModel::startPollBalance()
56+
{
57+
// This timer will be fired repeatedly to update the balance
58+
QTimer* timer = new QTimer(this);
59+
connect(timer, &QTimer::timeout, this, &WalletModel::pollBalanceChanged);
60+
timer->start(MODEL_UPDATE_DELAY);
61+
}
62+
6063
void WalletModel::updateStatus()
6164
{
6265
EncryptionStatus newEncryptionStatus = getEncryptionStatus();

src/qt/walletmodel.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,6 @@ class WalletModel : public QObject
235235
EncryptionStatus cachedEncryptionStatus;
236236
int cachedNumBlocks;
237237

238-
QTimer *pollTimer;
239-
240238
void subscribeToCoreSignals();
241239
void unsubscribeFromCoreSignals();
242240
void checkBalanceChanged(const interfaces::WalletBalances& new_balances);
@@ -272,6 +270,9 @@ class WalletModel : public QObject
272270
void canGetAddressesChanged();
273271

274272
public Q_SLOTS:
273+
/* Starts a timer to periodically update the balance */
274+
void startPollBalance();
275+
275276
/* Wallet status might have changed */
276277
void updateStatus();
277278
/* New transaction, or transaction changed status */

0 commit comments

Comments
 (0)