schrodinger.utils.qapplication module

Functions for managing the global QApplication instance.

Typical usage:

if __name__ == ‘__main__’:

af2.start_application(MyPanel.panel)

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

schrodinger.utils.qapplication.ApplicationMode

alias of schrodinger.utils.qapplication.RunMode

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

Bases: RuntimeError

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

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.

Parameters
  • 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.

Parameters
  • 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.

Raises
  • 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)

Returns

the application

Return type

QApplication or None

schrodinger.utils.qapplication.is_core_application()

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

Returns

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__’:

application.start_application(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 MyPanel.show() without having to call application.exec().

This is generally intended for use with GUI scripts.

Parameters
  • 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.

schrodinger.utils.qapplication.process_events(flags=None)

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

Parameters

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

Raises

RuntimeError – if there is no application

schrodinger.utils.qapplication.quit_application()

Quits the application.