92
92
}
93
93
94
94
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 ()))
97
100
98
101
102
+ # lru_cache keeps a reference to the QApplication instance, keeping it from
103
+ # being GC'd.
104
+ @functools .lru_cache (1 )
99
105
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 ()
131
132
132
133
try :
133
- qApp .setAttribute (QtCore .Qt .AA_UseHighDpiPixmaps ) # Only for Qt<6.
134
+ app .setAttribute (QtCore .Qt .AA_UseHighDpiPixmaps ) # Only for Qt<6.
134
135
except AttributeError :
135
136
pass
136
137
138
+ return app
139
+
137
140
138
141
def _allow_super_init (__init__ ):
139
142
"""
@@ -397,7 +400,7 @@ def _get_key(self, event):
397
400
398
401
def flush_events (self ):
399
402
# docstring inherited
400
- qApp .processEvents ()
403
+ QtWidgets . QApplication . instance () .processEvents ()
401
404
402
405
def start_event_loop (self , timeout = 0 ):
403
406
# docstring inherited
@@ -1001,7 +1004,7 @@ def trigger(self, *args):
1001
1004
class ToolCopyToClipboardQT (backend_tools .ToolCopyToClipboardBase ):
1002
1005
def trigger (self , * args , ** kwargs ):
1003
1006
pixmap = self .canvas .grab ()
1004
- qApp .clipboard ().setPixmap (pixmap )
1007
+ QtWidgets . QApplication . instance () .clipboard ().setPixmap (pixmap )
1005
1008
1006
1009
1007
1010
FigureManagerQT ._toolbar2_class = NavigationToolbar2QT
@@ -1015,5 +1018,6 @@ class _BackendQT(_Backend):
1015
1018
1016
1019
@staticmethod
1017
1020
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