From 14363f8f2fe75382fd3b7c291fc1b14c678a51a8 Mon Sep 17 00:00:00 2001
From: Calin Culianu <calin.culianu@gmail.com>
Date: Wed, 12 Dec 2018 00:53:55 +0200
Subject: [PATCH] [Qt] Got rid of qt.util.Timer class and instead replaced the
 functionality with the more efficient QTimer. Also added disconnection from
 the timer on window close.

(cherry picked from https://github.com/Electron-Cash/Electron-Cash/commit/19a21eb08d4c3bb665d4a3b50daf38d51b6589b3)
---
 electrum/gui/qt/__init__.py    |  6 +++++-
 electrum/gui/qt/main_window.py |  7 +++----
 electrum/gui/qt/util.py        | 13 -------------
 3 files changed, 8 insertions(+), 18 deletions(-)

diff --git a/electrum/gui/qt/__init__.py b/electrum/gui/qt/__init__.py
index 07e7e503d..4379c894a 100644
--- a/electrum/gui/qt/__init__.py
+++ b/electrum/gui/qt/__init__.py
@@ -105,7 +105,11 @@ class ElectrumGui(PrintError):
         self.efilter = OpenFileEventFilter(self.windows)
         self.app = QElectrumApplication(sys.argv)
         self.app.installEventFilter(self.efilter)
-        self.timer = Timer()
+        # timer
+        self.timer = QTimer(self.app)
+        self.timer.setSingleShot(False)
+        self.timer.setInterval(500)  # msec
+
         self.nd = None
         self.network_updated_signal_obj = QNetworkUpdatedSignalObject()
         self._num_wizards_in_progress = 0
diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py
index e8dad31ea..0db7fa05c 100644
--- a/electrum/gui/qt/main_window.py
+++ b/electrum/gui/qt/main_window.py
@@ -222,7 +222,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
         # update fee slider in case we missed the callback
         self.fee_slider.update()
         self.load_wallet(wallet)
-        self.connect_slots(gui_object.timer)
+        gui_object.timer.timeout.connect(self.timer_actions)
         self.fetch_alias()
 
     def on_history(self, b):
@@ -670,9 +670,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
             self.config.set_key('io_dir', os.path.dirname(fileName), True)
         return fileName
 
-    def connect_slots(self, sender):
-        sender.timer_signal.connect(self.timer_actions)
-
     def timer_actions(self):
         # Note this runs in the GUI thread
         if self.need_update.is_set():
@@ -3134,6 +3131,8 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
         if self.qr_window:
             self.qr_window.close()
         self.close_wallet()
+
+        self.gui_object.timer.timeout.disconnect(self.timer_actions)
         self.gui_object.close_window(self)
 
     def plugins_dialog(self):
diff --git a/electrum/gui/qt/util.py b/electrum/gui/qt/util.py
index 409cbc8ab..ff93b181c 100644
--- a/electrum/gui/qt/util.py
+++ b/electrum/gui/qt/util.py
@@ -48,19 +48,6 @@ expiration_values = [
 ]
 
 
-class Timer(QThread):
-    stopped = False
-    timer_signal = pyqtSignal()
-
-    def run(self):
-        while not self.stopped:
-            self.timer_signal.emit()
-            time.sleep(0.5)
-
-    def stop(self):
-        self.stopped = True
-        self.wait()
-
 class EnterButton(QPushButton):
     def __init__(self, text, func):
         QPushButton.__init__(self, text)