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
from schrodinger.Qt.QtCore import QtMsgType

_MSG_TYPES_TO_SUPPRESS = {QtMsgType.QtDebugMsg, QtMsgType.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.",
    # PANEL-20167 This warning indicates that QT_MAC_WANTS_LAYER is being set.
    # PyQt >=5.15.2 sets QT_MAC_WANTS_LAYER.
    "Layer-backing can not be explicitly controlled on 10.14 when built "
    "against the 10.14 SDK",
}
_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
[docs]def filter_qt_msg(msg: str) -> bool: """ :return: whether to filter out the Qt message """ return msg in _MESSAGES_TO_SUPPRESS
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 filter_qt_msg(msg): return elif msg.startswith("Could not parse stylesheet of object"): # We want invalid stylesheets to be exceptions rather than warnings raise RuntimeError(msg) elif _OLD_HANDLER is not None: _OLD_HANDLER(msg_type, context, msg) else: print(msg)