Esempio n. 1
0
 def _setup_loop(self):
     # if self.value is not None:
     self.timer = QTimer(self.control)
     self.timer.timeout.connect(self._update)
     print('fps', self.factory.fps)
     if self.factory.fps:
         self.timer.setInterval(1000 / self.factory.fps)
     self.timer.start()
Esempio n. 2
0
def delete_widget(widget, timeout=1.0):
    """ Context manager that executes the event loop so that we can safely
    delete a Qt widget.
    """

    app = get_app_qt4()

    timer = QTimer()
    timer.setSingleShot(True)
    timer.setInterval(round(timeout * 1000))
    timer.timeout.connect(app.quit)
    widget.destroyed.connect(app.quit)

    yield

    timer.start()
    app.exec_()

    if not timer.isActive():
        # We exited the event loop on timeout.
        msg = "Could not destroy widget before timeout: {!r}"
        raise AssertionError(msg.format(widget))
Esempio n. 3
0
    def run_until(self, object, trait, condition, timeout):
        """
        Run event loop until the given condition holds true, or until timeout.

        The condition is re-evaluated, with the object as argument, every time
        the trait changes.

        Parameters
        ----------
        object : traits.has_traits.HasTraits
            Object whose trait we monitor.
        trait : str
            Name of the trait to monitor for changes.
        condition
            Single-argument callable, returning a boolean. This will be
            called with *object* as the only input.
        timeout : float
            Number of seconds to allow before timing out with an exception.

        Raises
        ------
        RuntimeError
            If timeout is reached, regardless of whether the condition is
            true or not at that point.
        """

        qt_app = self.qt_app

        timeout_in_ms = round(1000.0 * timeout)
        timeout_timer = QTimer()
        timeout_timer.setSingleShot(True)
        timeout_timer.setInterval(timeout_in_ms)

        def stop_on_timeout():
            qt_app.exit(1)

        def stop_if_condition(event):
            if condition(object):
                qt_app.exit(0)

        object.observe(stop_if_condition, trait)
        try:
            # The condition may have become True before we
            # started listening to changes. So start with a check.
            if condition(object):
                timed_out = 0
            else:
                timeout_timer.timeout.connect(stop_on_timeout)
                timeout_timer.start()
                try:
                    # Qt wrapper support is a bit of a mess here. PyQt6 has
                    # only "exec". PyQt5 and PySide6 support both "exec" and
                    # "exec_", with "exec" the preferred spelling. PyQt4 and
                    # PySide2 support only "exec_".
                    try:
                        exec_method = qt_app.exec
                    except AttributeError:
                        exec_method = qt_app.exec_
                    timed_out = exec_method()
                finally:
                    timeout_timer.stop()
                    timeout_timer.timeout.disconnect(stop_on_timeout)
        finally:
            object.observe(stop_if_condition, trait, remove=True)

        if timed_out:
            raise RuntimeError("run_until timed out after {} seconds. "
                               "At timeout, condition was {}.".format(
                                   timeout, condition(object)))
Esempio n. 4
0
 def __init__(self, **traits):
     traits.setdefault("_timer", QTimer())
     super(PyfaceTimer, self).__init__(**traits)
     self._timer.timeout.connect(self.perform)