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 QtCoreclass 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 changeddef __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 QtCoreclass testSignals(QtCore.QObject):signal = QtCore.Signal();def __init__(self):self.is_running = FalseQtCore.QObject.__init__(self)self.start()def emitMe(self):self.is_running = Falseself.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 = Truedef stop(self):self._timer.cancel()self.is_running = Falseclass testConnection():@QtCore.Slot()def receiveMe(self):print('Signal received')# signal is emmited when data is changeddef __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.