Source code for schrodinger.infra.qt_message_handler
"""
A Qt message handler that suppresses debugging and info output as well as any
warnings or errors that we can't otherwise fix.
"""
from schrodinger.Qt import QtCore
_MSG_TYPES_TO_SUPPRESS = {QtCore.QtDebugMsg, QtCore.QtInfoMsg}
_MESSAGES_TO_SUPPRESS = {
# This warning can come from matplotlib 2.2.4, so we suppress it until
# we upgrade to matplotlib 3
"Attribute Qt::AA_EnableHighDpiScaling must be set before QCoreApplication "
"is created.",
}
_OLD_HANDLER = None
_HANDLER_INSTALLED = False
[docs]def install_handler():
"""
Install the message handler. Is a no-op if called more than once.
"""
global _OLD_HANDLER, _HANDLER_INSTALLED
if _HANDLER_INSTALLED:
return
_OLD_HANDLER = QtCore.qInstallMessageHandler(_handler)
_HANDLER_INSTALLED = True
def _handler(msg_type, context, msg):
"""
Handle Qt messages. Message types in _MSG_TYPES_TO_SUPPRESS and messages in
_MESSAGES_TO_SUPPRESS will be ignored. All other messages will be sent to
the previous custom handler if one exists. If there's no previous custom
handler, then the messages will be printed to standard output (which is the
default Qt behavior).
:param msg_type: The message type. Given as an integer representing a
QtCore.QtMsgType enum value.
:type msg_type: int
:param context: The context of the message
:type context: QtMessageLogContext
:param msg: The message itself
:type msg: str
"""
if msg_type in _MSG_TYPES_TO_SUPPRESS or msg in _MESSAGES_TO_SUPPRESS:
return
elif _OLD_HANDLER is not None:
_OLD_HANDLER(msg_type, context, msg)
else:
print(msg)