From 73c0bd69cb242c35fa9b8e1f87c018ec0081e4f7 Mon Sep 17 00:00:00 2001
From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
Date: Wed, 11 Oct 2023 21:29:13 +0200
Subject: [PATCH] Close all plot windows of a blocking show() on Ctrl+C

Addresses the Qt part of #23385.

It appears that `qapp.quit()` does not automatically close the windows
of the app. We therefore do it explicitly.

A unit test for this would be quite complex. Test this
by hand by Ctrl+C in an interactive shell.
---
 lib/matplotlib/backends/qt_compat.py | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lib/matplotlib/backends/qt_compat.py b/lib/matplotlib/backends/qt_compat.py
index d587223ab9cf..98f9f9572d69 100644
--- a/lib/matplotlib/backends/qt_compat.py
+++ b/lib/matplotlib/backends/qt_compat.py
@@ -163,7 +163,7 @@ def _exec(obj):
 
 
 @contextlib.contextmanager
-def _maybe_allow_interrupt(qapp):
+def _maybe_allow_interrupt(qapp_or_eventloop):
     """
     This manager allows to terminate a plot by sending a SIGINT. It is
     necessary because the running Qt backend prevents Python interpreter to
@@ -215,7 +215,9 @@ def _may_clear_sock(*args):
     def handle(*args):
         nonlocal handler_args
         handler_args = args
-        qapp.quit()
+        if hasattr(qapp_or_eventloop, 'closeAllWindows'):
+            qapp_or_eventloop.closeAllWindows()
+        qapp_or_eventloop.quit()
 
     signal.signal(signal.SIGINT, handle)
     try: