def _QTest_qWaitForWindowActive(widget, timeout=1000): # A Qt5 compatible (probably) QTest.qWaitForWindowActive(QWidget, int) # (mostly copied from qtestsystem.h in qt5/qtbase) from AnyQt.QtCore import \ Qt, QCoreApplication, QEventLoop, QElapsedTimer, QEvent window = widget.window() timer = QElapsedTimer() timer.start() while not window.isActiveWindow(): remaining = timeout - timer.elapsed() if remaining <= 0: break QCoreApplication.processEvents(QEventLoop.AllEvents, remaining) QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete) QTest.qSleep(10) # See the explanation in qtestsystem.h if window.isActiveWindow(): wait_no = 0 while window.pos().isNull(): if wait_no > timeout // 10: break wait_no += 1 QTest.qWait(10) return window.isActiveWindow()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__searchline = QLineEdit(self, visible=False, frame=False) self.__searchline.setAttribute(Qt.WA_MacShowFocusRect, False) self.__searchline.setFocusProxy(self) self.__popup = None # type: Optional[QAbstractItemModel] self.__proxy = None # type: Optional[QSortFilterProxyModel] self.__popupTimer = QElapsedTimer() self.setFocusPolicy(Qt.ClickFocus | Qt.TabFocus)
def __init__(self, parent=None, **kwargs): self.__maximumContentsLength = MAXIMUM_CONTENTS_LENGTH self.__searchline = QLineEdit(visible=False, frame=False) self.__searchline.setAttribute(Qt.WA_MacShowFocusRect, False) self.__popup = None # type: Optional[QAbstractItemModel] self.__proxy = None # type: Optional[QSortFilterProxyModel] self.__popupTimer = QElapsedTimer() super().__init__(parent, **kwargs) self.__searchline.setParent(self) self.__searchline.setFocusProxy(self) self.setFocusPolicy(Qt.ClickFocus | Qt.TabFocus)
def get_output(self, widget, signal_name, timeout=DEFAULT_TIMEOUT): if not isinstance(signal_name, str): signal_name = signal_name.name elapsed = QElapsedTimer() if widget.isInvalidated(): elapsed.start() spy = QSignalSpy(widget.invalidatedStateChanged) assert spy.wait(timeout) timeout = timeout - elapsed.elapsed() value = self.outputs.get((widget, signal_name)) if isinstance(value, _Invalidated) and timeout >= 0: spy = QSignalSpy(value.completed) assert spy.wait(timeout), "Failed to get output in the specified timeout" assert len(spy) == 1 value = spy[0][0] return value
def _QTest_qWaitForWindowExposed(widget, timeout=1000): # A Qt5 compatible (probably) QTest.qWaitForWindowExposed(QWidget, int) # (mostly copied from qtestsystem.h in qt5/qtbase) from AnyQt.QtCore import \ Qt, QCoreApplication, QEventLoop, QElapsedTimer, QEvent window = widget.window() timer = QElapsedTimer() timer.start() # Is widget.testAttribute(Qt.WA_Mapped) a suitable replacement for # QWindow.isExposed in Qt5?? # Not exactly. In Qt5 # window().testAttribute(Qt.WA_Mapped) == window().windowHandle.isExposed() # but both are False if a window is fully obscured by other windows, # in Qt4 there is no difference if a window is obscured. while not window.testAttribute(Qt.WA_Mapped): remaining = timeout - timer.elapsed() if remaining <= 0: break QCoreApplication.processEvents(QEventLoop.AllEvents, remaining) QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete) QTest.qSleep(10) return window.testAttribute(Qt.WA_Mapped)