Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ceae66a

Browse files
committed
Deprecate backend_qt.qApp.
This global has subtly different semantics from QtWidgets.QApplication.instance() (it is only updated when the private _create_qApp is called), and therefore seems not worth exposing.
1 parent 3c6efec commit ceae66a

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``backend_qt.qApp``
2+
~~~~~~~~~~~~~~~~~~~
3+
... is deprecated. Use ``QtWidgets.QApplication.instance()`` instead.

lib/matplotlib/backends/backend_qt.py

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -92,48 +92,51 @@
9292
}
9393

9494

95-
# make place holder
96-
qApp = None
95+
@_api.caching_module_getattr
96+
class __getattr__:
97+
qApp = _api.deprecated(
98+
"3.6", alternative="QtWidgets.QApplication.instance()")(
99+
property(lambda self: QtWidgets.QApplication.instance()))
97100

98101

102+
# lru_cache keeps a reference to the QApplication instance, keeping it from
103+
# being GC'd.
104+
@functools.lru_cache(1)
99105
def _create_qApp():
100-
"""
101-
Only one qApp can exist at a time, so check before creating one.
102-
"""
103-
global qApp
104-
105-
if qApp is None:
106-
app = QtWidgets.QApplication.instance()
107-
if app is None:
108-
# display_is_valid returns False only if on Linux and neither X11
109-
# nor Wayland display can be opened.
110-
if not mpl._c_internal_utils.display_is_valid():
111-
raise RuntimeError('Invalid DISPLAY variable')
112-
try:
113-
QtWidgets.QApplication.setAttribute(
114-
QtCore.Qt.AA_EnableHighDpiScaling)
115-
except AttributeError: # Only for Qt>=5.6, <6.
116-
pass
117-
try:
118-
QtWidgets.QApplication.setHighDpiScaleFactorRoundingPolicy(
119-
QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
120-
except AttributeError: # Only for Qt>=5.14.
121-
pass
122-
qApp = QtWidgets.QApplication(["matplotlib"])
123-
if sys.platform == "darwin":
124-
image = str(cbook._get_data_path('images/matplotlib.svg'))
125-
icon = QtGui.QIcon(image)
126-
qApp.setWindowIcon(icon)
127-
qApp.lastWindowClosed.connect(qApp.quit)
128-
cbook._setup_new_guiapp()
129-
else:
130-
qApp = app
106+
app = QtWidgets.QApplication.instance()
107+
108+
# Create a new QApplication and configure if if non exists yet, as only one
109+
# QApplication can exist at a time.
110+
if app is None:
111+
# display_is_valid returns False only if on Linux and neither X11
112+
# nor Wayland display can be opened.
113+
if not mpl._c_internal_utils.display_is_valid():
114+
raise RuntimeError('Invalid DISPLAY variable')
115+
try:
116+
QtWidgets.QApplication.setAttribute(
117+
QtCore.Qt.AA_EnableHighDpiScaling)
118+
except AttributeError: # Only for Qt>=5.6, <6.
119+
pass
120+
try:
121+
QtWidgets.QApplication.setHighDpiScaleFactorRoundingPolicy(
122+
QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
123+
except AttributeError: # Only for Qt>=5.14.
124+
pass
125+
app = QtWidgets.QApplication(["matplotlib"])
126+
if sys.platform == "darwin":
127+
image = str(cbook._get_data_path('images/matplotlib.svg'))
128+
icon = QtGui.QIcon(image)
129+
app.setWindowIcon(icon)
130+
app.lastWindowClosed.connect(app.quit)
131+
cbook._setup_new_guiapp()
131132

132133
try:
133-
qApp.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps) # Only for Qt<6.
134+
app.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps) # Only for Qt<6.
134135
except AttributeError:
135136
pass
136137

138+
return app
139+
137140

138141
def _allow_super_init(__init__):
139142
"""
@@ -397,7 +400,7 @@ def _get_key(self, event):
397400

398401
def flush_events(self):
399402
# docstring inherited
400-
qApp.processEvents()
403+
QtWidgets.QApplication.instance().processEvents()
401404

402405
def start_event_loop(self, timeout=0):
403406
# docstring inherited
@@ -1001,7 +1004,7 @@ def trigger(self, *args):
10011004
class ToolCopyToClipboardQT(backend_tools.ToolCopyToClipboardBase):
10021005
def trigger(self, *args, **kwargs):
10031006
pixmap = self.canvas.grab()
1004-
qApp.clipboard().setPixmap(pixmap)
1007+
QtWidgets.QApplication.instance().clipboard().setPixmap(pixmap)
10051008

10061009

10071010
FigureManagerQT._toolbar2_class = NavigationToolbar2QT
@@ -1015,5 +1018,6 @@ class _BackendQT(_Backend):
10151018

10161019
@staticmethod
10171020
def mainloop():
1018-
with _maybe_allow_interrupt(qApp):
1019-
qt_compat._exec(qApp)
1021+
qapp = QtWidgets.QApplication.instance()
1022+
with _maybe_allow_interrupt(qapp):
1023+
qt_compat._exec(qapp)

0 commit comments

Comments
 (0)