8000 FIX: fixed the viewer being unusable after showing a matplotlib plot … · larray-project/larray-editor@48b86fb · GitHub
[go: up one dir, main page]

Skip to content

Commit 48b86fb

Browse files
committed
FIX: fixed the viewer being unusable after showing a matplotlib plot (closes #261)
Matplotlib seems to leave a Qt application behind with an invisible window opened if the user mouses over the figure before closing it. Obviously, the Qt app event loop is not running anymore.
1 parent 1dcb222 commit 48b86fb

File tree

2 files changed

+20
-49
lines changed

2 files changed

+20
-49
lines changed
Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,6 @@
11
.. py:currentmodule:: larray_editor
22

3-
Syntax changes
4-
^^^^^^^^^^^^^^
5-
6-
* renamed ``MappingEditor.old_method_name()`` to :py:obj:`MappingEditor.new_method_name()` (closes :editor_issue:`1`).
7-
8-
* renamed ``old_argument_name`` argument of :py:obj:`MappingEditor.method_name()` to ``new_argument_name``.
9-
10-
11-
Backward incompatible changes
12-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13-
14-
* other backward incompatible changes
15-
16-
17-
New features
18-
^^^^^^^^^^^^
19-
20-
* added a feature (see the :ref:`miscellaneous section <misc_editor>` for details).
21-
22-
* added another feature in the editor (closes :editor_issue:`1`).
23-
24-
.. note::
25-
26-
- It works for foo bar !
27-
- It does not work for foo baz !
28-
29-
30-
.. _misc_editor:
31-
32-
Miscellaneous improvements
33-
^^^^^^^^^^^^^^^^^^^^^^^^^^
34-
35-
* improved something.
36-
37-
383
Fixes
394
^^^^^
405

41-
* fixed something (closes :editor_issue:`1`).
6+
* fixed the viewer being unusable after showing a matplotlib plot (closes :editor_issue:`261`).

larray_editor/api.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from qtpy.QtWidgets import QApplication
88
import larray as la
99

10-
from larray_editor.editor import REOPEN_LAST_FILE, MappingEditor, ArrayEditor
10+
from larray_editor.editor import REOPEN_LAST_FILE, MappingEditor, ArrayEditor, AbstractEditor
1111
from larray_editor.traceback_tools import extract_stack, extract_tb, StackSummary
1212

1313
__all__ = ['view', 'edit', 'debug', 'compare', 'REOPEN_LAST_FILE', 'run_editor_on_exception']
@@ -35,10 +35,12 @@ def _show_dialog(app_name, create_dialog_func, *args, **kwargs):
3535
# activeWindow is defined only if the Window has keyboard focus,
3636
# so it could be None even if the app has a window open
3737
parent = qt_app.activeWindow()
38-
if parent is None:
39-
app_windows = qt_app.topLevelWindows()
40-
if len(app_windows) > 0:
41-
parent = app_windows[0]
38+
if not isinstance(parent, AbstractEditor):
39+
# We use topLevelWidgets and not topLevelWindows because the later
40+
# returns QWindow instances whereas we actually need a QWidget
41+
# instance (of which QMainWindow is a descendant) as parent.
42+
app_windows = [widget for widget in qt_app.topLevelWidgets() if isinstance(widget, AbstractEditor)]
43+
parent = app_windows[0] if len(app_windows) else None
4244

4345
if 'depth' in kwargs:
4446
kwargs['depth'] += 1
@@ -49,14 +51,18 @@ def _show_dialog(app_name, create_dialog_func, *args, **kwargs):
4951

5052
dlg.show()
5153

52-
# We used to test whether qt_app was None, but it failed when an instance existed with no
53-
# event loop started such as when running code via PyCharm's "Run File in Python Console"
54-
# feature. See https://github.com/larray-project/larray-editor/issues/253.
55-
56-
# We use whether any Qt window exists in the application as a proxy to test whether
57-
# Qt main event loop runs. This is probably wrong for an application which uses Qt event
58-
# system but not its GUI (i.e. using QtCoreApplication) but I haven't found any way
59-
# to check explicitly whether the main event loop is already running.
54+
# We used to test whether qt_app was None, but it failed when an
55+
# Application instance existed with no event loop running such as when
56+
# running code via PyCharm's "Run File in Python Console" feature
57+
# (see issue #253) or after showing matplotlib figures (see issue #261).
58+
59+
# We have not found any way to explicitly check whether the main event loop
60+
# is already running, so we now assume that if the parent window is not
61+
# a descendant of our own MappingEditor, no event loop is running, as it is
62+
# unlikely another Qt application calls the *api functions* instead of
63+
# embedding the widget. Note that jupyter qtconsole works, because the
64+
# Python kernel process is not the same as the process running the
65+
# interface, so QApplication.instance() returns None in the kernel process.
6066
if parent is None:
6167
# We do not use install_except_hook/restore_except_hook so that we can restore the hook actually used when
6268
# this function is called instead of the one which was used when the module was loaded.

0 commit comments

Comments
 (0)
0