schrodinger.utils.qapplication module

Functions for managing the global QApplication instance.

Typical usage:

if __name__ == ‘__main__’:


(Note that the start_application function is available from the af2 namespace)


alias of schrodinger.utils.qapplication.RunMode

exception schrodinger.utils.qapplication.CantCreateQApplicationError(msg='', *args, **kwargs)

Bases: RuntimeError

__init__(msg='', *args, **kwargs)

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

schrodinger.utils.qapplication.require_application(func=None, create=False, use_qtcore_app=False)

Use this decorator on functions that require a QApplication to run. When the decorated function is called, this will check whether a QApplication exists. If it does not, a RuntimeError will be raised unless create=True, which will cause a QApplication to be created.

  • func – the function to decorate

  • create (bool) – whether to create a QApplication if one does not exist

  • use_qtcore_app (bool) – Whether to create the application using the QtCore module instead of the QtWidgets module.

schrodinger.utils.qapplication.get_application(create=True, use_qtcore_app=False)

Gets the global QApplication instance. By default, creates one if none exists.

  • create (bool) – Whether to create a new application if none exists.

  • use_qtcore_app – Whether to create the application using the QtCore module instead of the QtWidgets module.

  • CantCreateQApplicationError – if use_qtcore_app is False but a QCoreApplication already exists. In the opposite case (use_qtcore_app is True but a QApplication already exists), no traceback is thrown (because QApplication is a substitutable subclass of QCoreApplication)

  • CantCreateQApplicationError – if use_qtcore_app is False and importing QtWidgets fails (e.g. due to missing graphics libraries)


the application

Return type

QApplication or None


Return whether the application instance is a QtCore application (as opposed to a QtWidgets application).


Whether there is a QtCore application or None if there’s no application

Return type

bool or NoneType

schrodinger.utils.qapplication.start_application(main_callable, use_qtcore_app=False)

Begins the application’s event loop using the exec method. The main callable is called via timer from within the event loop. This function is meant to be used when running in standalone scripts as follows:

if __name__ == ‘__main__’:


Using this function to launch standalone scripts/panels more closely mimics the way Python is run under Maestro. For example, a panel may be presented with without having to call application.exec().

This is generally intended for use with GUI scripts.

  • main_callable (callable) – the function/method to be run in the event loop, commonly a module level main function or MyPanelClass.panel

  • use_qtcore_app (bool) – Whether to create the application using the QtCore module instead of the QtWidgets module. Set to True for scripts that are non-GUI and/or need to run on machines that have no display.

schrodinger.utils.qapplication.run_application(main_callable, use_qtcore_app=True)

This function is the same as start_application with two important differences:

  1. Creates a QCoreApplication by default

  2. Quits the application as soon as the main_callable function returns

    and does a system exit with that return value

This is generally intended for use by non-GUI, commandline scripts.


Calls processEvents on the main event loop. Requires an application.


flags – passed to processEvents. See QCoreApplication.processEvents for documentation


RuntimeError – if there is no application


Quits the application.