schrodinger.utils.qt_utils module

Utility classes and functions for use with Qt objects (such as QObjects). Note that contrary to schrodinger.ui.qt.utils, these utilities do not rely on QtGui or QtWidgets. This allows these utilities to be used on headless servers which shouldn’t import QtGui or QtWidgets.

class schrodinger.utils.qt_utils.suppress_signals(*args, suppress=True)

Bases: object

A context manager to prevent signals from being emitted from the specified widget(s). All widgets to be suppressed should be passed as arguments.

__init__(*args, suppress=True)

Create a suppress_signals instance to suppress signals for all the widgets given as arguments.

Parameters:bool (suppress) –

If True, suppress signals. If False, don’t. Allows constructs such as -

with suppress_signal(mywidget, suppress=self.resetting)
exception schrodinger.utils.qt_utils.SignalTimeoutException

Bases: RuntimeError

__init__

Initialize self. See help(type(self)) for accurate signature.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

schrodinger.utils.qt_utils.wait_for_signal(signal, timeout=None)

Uses an event loop to wait until a signal is emitted. If the signal is not emitted within a specified timeout duration, a SignalTimeoutException is raised.

Parameters:
  • signal – the signal to wait for
  • timeout (float) – number of seconds to wait for the signal before timing out
Returns:

the args emitted with the signal, if any. If there is only one arg emitted, it will be returned directly. If there are more, they will be return as a tuple.

schrodinger.utils.qt_utils.call_func_and_wait_for_signal(func, signal, timeout=None)

Calls the specified function and then waits for the signal. The function is called in such a way that the signal is guaranteed to be caught, even if the signal is emitted before the function returns.

Parameters:func – the function to call

See wait_for_signal for additional parameter documentation.

class schrodinger.utils.qt_utils.ABCMetaQObject(name, bases, attrs)

Bases: abc.ABCMeta, sip.wrappertype

Metaclass to allow a derived object to be a QObject and an abc.

Usage:

class MyClass(QtCore.QObject, metaclass=ABCMetaQObject):
__init__(name, bases, attrs)

Initialize self. See help(type(self)) for accurate signature.

mro()

Return a type’s method resolution order.

register(subclass)

Register a virtual subclass of an ABC.

Returns the subclass, to allow usage as a class decorator.

schrodinger.utils.qt_utils.get_signals(source)

Utility method for iterating through the signals on a QObject.

Parameters:
  • source (QtCore.QObject or instance thereof) – Any object or class with signals
  • filter (tuple of strings) – Any signals to ignore
Return type:

list of tuples

Returns:

A list of (name, signal) tuples

class schrodinger.utils.qt_utils.SignalAndSlot(signal, slot)

Bases: object

A composite object to manage a single signal/slot pair. Usage:

class ClassName(QtWidgets.QWidget):

    fooChangedSignal = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(ClassName, self).__init__(parent)
        self.fooChanged = qt_utils.SignalAndSlot(self.fooChangedSignal,
                                                 self.fooChangedSlot)

    def fooChangedSlot(self):
        pass
__init__(signal, slot)

Create an object that acts as both a signal and a slot

Parameters:
  • signal (PyQt5.QtCore.pyqtSignal) – The signal object
  • slot (function) – The slot object
emit(*args, **kwargs)
connect(*args, **kwargs)
disconnect(*args, **kwargs)
schrodinger.utils.qt_utils.add_enums_as_attributes(enum_)

A class decorator that takes in an enum and aliases its members on the decorated class. For example:

Shape = enum.Enum('Shape', 'SQUARE TRIANGLE CIRCLE')
@qt_utils.add_enums_as_attributes(Shape)
class Foo:
    pass

assert Foo.SQUARE is Shape.SQUARE
assert Foo.TRIANGLE is Shape.TRIANGLE
assert Foo.CIRCLE is Shape.CIRCLE
schrodinger.utils.qt_utils.exit_event_loop_on_exception(func, *args, **kwargs)

Decorates a function that passes an event_loop keyword so if func throws an exception, the event loop will exit. The exception is accesible in get_last_exception. Example usage:

@exit_event_loop_on_exception def slot(event_loop=None):

… event_loop.quit()

event_loop = schrodinger.QtCore.QEventLoop() timer = schrodinger.QtCore.QTimer() timer.timeout.connect(functools.partial(event_loop=event_loop)) timer.start(1) event_loop.exec() exc = get_last_exception() if exc:

raise exc
schrodinger.utils.qt_utils.get_last_exception()

Returns an exception if one was thrown previously in exit_event_loop_on_exception. Returns None if no exception was thrown. Calling this function resets the exception state.