()
| 91 | # being GC'd. |
| 92 | @functools.lru_cache(1) |
| 93 | def _create_qApp(): |
| 94 | app = QtWidgets.QApplication.instance() |
| 95 | |
| 96 | # Create a new QApplication and configure it if none exists yet, as only |
| 97 | # one QApplication can exist at a time. |
| 98 | if app is None: |
| 99 | # display_is_valid returns False only if on Linux and neither X11 |
| 100 | # nor Wayland display can be opened. |
| 101 | if not mpl._c_internal_utils.display_is_valid(): |
| 102 | raise RuntimeError('Invalid DISPLAY variable') |
| 103 | |
| 104 | # Check to make sure a QApplication from a different major version |
| 105 | # of Qt is not instantiated in the process |
| 106 | if QT_API in {'PyQt6', 'PySide6'}: |
| 107 | other_bindings = ('PyQt5', 'PySide2') |
| 108 | qt_version = 6 |
| 109 | elif QT_API in {'PyQt5', 'PySide2'}: |
| 110 | other_bindings = ('PyQt6', 'PySide6') |
| 111 | qt_version = 5 |
| 112 | else: |
| 113 | raise RuntimeError("Should never be here") |
| 114 | |
| 115 | for binding in other_bindings: |
| 116 | mod = sys.modules.get(f'{binding}.QtWidgets') |
| 117 | if mod is not None and mod.QApplication.instance() is not None: |
| 118 | other_core = sys.modules.get(f'{binding}.QtCore') |
| 119 | _api.warn_external( |
| 120 | f'Matplotlib is using {QT_API} which wraps ' |
| 121 | f'{QtCore.qVersion()} however an instantiated ' |
| 122 | f'QApplication from {binding} which wraps ' |
| 123 | f'{other_core.qVersion()} exists. Mixing Qt major ' |
| 124 | 'versions may not work as expected.' |
| 125 | ) |
| 126 | break |
| 127 | if qt_version == 5: |
| 128 | try: |
| 129 | QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) |
| 130 | except AttributeError: # Only for Qt>=5.6, <6. |
| 131 | pass |
| 132 | try: |
| 133 | QtWidgets.QApplication.setHighDpiScaleFactorRoundingPolicy( |
| 134 | QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough) |
| 135 | except AttributeError: # Only for Qt>=5.14. |
| 136 | pass |
| 137 | app = QtWidgets.QApplication(["matplotlib"]) |
| 138 | if sys.platform == "darwin": |
| 139 | image = str(cbook._get_data_path('images/matplotlib.svg')) |
| 140 | icon = QtGui.QIcon(image) |
| 141 | app.setWindowIcon(icon) |
| 142 | app.setQuitOnLastWindowClosed(True) |
| 143 | cbook._setup_new_guiapp() |
| 144 | if qt_version == 5: |
| 145 | app.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps) |
| 146 | |
| 147 | return app |
| 148 | |
| 149 | |
| 150 | def _allow_interrupt_qt(qapp_or_eventloop): |
searching dependent graphs…