Skip to content

Instantly share code, notes, and snippets.

@ales-erjavec
Last active October 3, 2016 08:36
Show Gist options
  • Save ales-erjavec/950c852ab468676e6f09d97ecc57d8a6 to your computer and use it in GitHub Desktop.
Save ales-erjavec/950c852ab468676e6f09d97ecc57d8a6 to your computer and use it in GitHub Desktop.
"""
A QSignalSpy like object, which is sadly missing from PyQt4 (but is exposed in PyQt5)
No guaranties it behaves as the real thing under all circumstances.
"""
from PyQt4.QtCore import QObject, QEventLoop, QTimer
class QSignalSpy(QObject):
"""
QSignalSpy-like object
"""
def __init__(self, boundsig):
self.__boundsig = boundsig # type: pyqtBoundSignal
self.__boundsig.connect(lambda *args: self.__record(*args))
self.__recorded = [] # List[List[Any]]
self.__loop = QEventLoop()
self.__timer = QTimer(singleShot=True)
self.__timer.timeout.connect(self.__loop.quit)
def __record(self, *args):
self.__recorded.append(list(args))
if self.__loop.isRunning():
self.__loop.quit()
def wait(self, timeout):
count = len(self)
self.__timer.stop()
self.__timer.setInterval(timeout)
self.__timer.start()
self.__loop.exec_()
self.__timer.stop()
return len(self) != count
def __getitem__(self, index):
return self.__recorded[index]
def __len__(self):
return len(self.__recorded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment