Skip to content

QMessageHandler

A context manager that captures the messages that Qt logs (with qDebug, qWarning, qCritical, and so on), instead of printing them, for example to check for them in tests, or to send them to Python's logging.

QMessageHandler

QMessageHandler(logger: Logger | None = None)

A context manager to intercept messages from Qt.

Parameters:

  • logger (Logger, default: None ) –

    If provided, intercepted messages will be logged with logger at the corresponding python log level, by default None

Attributes:

  • records (list of tuple) –

    Captured messages. This is a 3-tuple of: (log_level: int, message: str, context: dict)

Examples:

>>> handler = QMessageHandler()
>>> handler.install()  # now all Qt output will be available at mh.records
>>> with QMessageHandler() as handler:  # temporarily install
...     ...
>>> logger = logging.getLogger(__name__)
>>> with QMessageHandler(logger):  # re-route Qt messages to a python logger.
...     ...

install

install()

Install this handler (override the current QtMessageHandler).

uninstall

uninstall()

Uninstall this handler, restoring the previous handler.