Skip to content

Don't import QtGui when loading QtCore unless required #496

@StSav012

Description

@StSav012

To make up mightBeRichText, QtGui is being imported in the following lines:

qtpy/qtpy/QtCore.py

Lines 103 to 112 in b851e00

# Missing QtGui utility functions on Qt
if getattr(Qt, "mightBeRichText", None) is None:
try:
from PySide2.QtGui import Qt as guiQt
Qt.mightBeRichText = guiQt.mightBeRichText
del guiQt
except ImportError:
# Fails with PySide2 5.12.0
pass

qtpy/qtpy/QtCore.py

Lines 132 to 137 in b851e00

# Missing QtGui utility functions on Qt
if getattr(Qt, "mightBeRichText", None) is None:
from PySide6.QtGui import Qt as guiQt
Qt.mightBeRichText = guiQt.mightBeRichText
del guiQt

I suppose the function is not something widely used, so the imports slow the module initiating down.

I'd implement the function this way (and similarly for PySide2):

    # Missing QtGui utility functions on Qt
    if not hasattr(Qt, "mightBeRichText"):
        def _mightBeRichText(*args, **kwargs):
            from PySide6.QtGui import Qt as guiQt

            return guiQt.mightBeRichText(*args, **kwargs)

        Qt.mightBeRichText = _mightBeRichText

Furthermore, this seems to bypass PYSIDE-2898.

But then, what about convertFromPlainText, which is also defined in QtGui.Qt?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions