I need to emit a signal periodically. A timer executes certain function, which emits the signal that I want. For some reason this function is not being emitted. I was able to reproduce the error on minimal code (see further down). If I do the same without the timer everything works:
from threading import Timer
import time
from PySide import QtCore
class testSignals(QtCore.QObject):
signal = QtCore.Signal();
def __init__(self):
QtCore.QObject.__init__(self)
def run(self):
self.signal.emit()
class testConnection():
@QtCore.Slot()
def receiveMe(self):
print('Signal received')
# signal is emmited when data is changed
def __init__(self):
test = testSignals()
test.signal.connect(self.receiveMe)
test.run();
test = testConnection()
I was able to reproduce my problem in the following code:
from threading import Timer
import time
from PySide import QtCore
class testSignals(QtCore.QObject):
signal = QtCore.Signal();
def __init__(self):
self.is_running = False
QtCore.QObject.__init__(self)
self.start()
def emitMe(self):
self.is_running = False
self.start()
# print("emitMe")
self.signal.emit()
def start(self):
if not self.is_running:
self._timer = Timer(0.2, self.emitMe)
self._timer.start()
self.is_running = True
def stop(self):
self._timer.cancel()
self.is_running = False
class testConnection():
@QtCore.Slot()
def receiveMe(self):
print('Signal received')
# signal is emmited when data is changed
def __init__(self):
test = testSignals()
test.signal.connect(self.receiveMe)
test.start();
time.sleep(2.0)
test.stop();
test = testConnection()
the phrase "Signal received" does not get printed on the screen.
As @ekhumoro said in the comments, QTimer helped a lot. I needed an event-loop. I'm posting the code to make the answer valid. I rewrote it using QTimer, which made everything simpler really. Note that I need to call QCoreApplication to run the threads. Again, this is just the minimal code to reproduce what I needed to do.
it produces: