Last active
October 3, 2016 08:36
-
-
Save ales-erjavec/950c852ab468676e6f09d97ecc57d8a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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