Skip to content

Commit a8f5026

Browse files
committed
gui: Fix start timer from non QThread
1 parent 561a7d3 commit a8f5026

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/qt/walletcontroller.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wal
108108
wallet_model->setParent(this);
109109
m_wallets.push_back(wallet_model);
110110

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

src/qt/walletmodel.cpp

Lines changed: 8 additions & 5 deletions
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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,6 @@ class WalletModel : public QObject
255255
EncryptionStatus cachedEncryptionStatus;
256256
int cachedNumBlocks;
257257

258-
QTimer *pollTimer;
259-
260258
void subscribeToCoreSignals();
261259
void unsubscribeFromCoreSignals();
262260
void checkBalanceChanged(const interfaces::WalletBalances& new_balances);
@@ -292,6 +290,9 @@ class WalletModel : public QObject
292290
void canGetAddressesChanged();
293291

294292
public Q_SLOTS:
293+
/* Starts a timer to periodically update the balance */
294+
void startPollBalance();
295+
295296
/* Wallet status might have changed */
296297
void updateStatus();
297298
/* New transaction, or transaction changed status */

0 commit comments

Comments
 (0)