7
7
from qtpy .QtWidgets import QApplication
8
8
import larray as la
9
9
10
- from larray_editor .editor import REOPEN_LAST_FILE , MappingEditor , ArrayEditor
10
+ from larray_editor .editor import REOPEN_LAST_FILE , MappingEditor , ArrayEditor , AbstractEditor
11
11
from larray_editor .traceback_tools import extract_stack , extract_tb , StackSummary
12
12
13
13
__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):
35
35
# activeWindow is defined only if the Window has keyboard focus,
36
36
# so it could be None even if the app has a window open
37
37
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
42
44
43
45
if 'depth' in kwargs :
44
46
kwargs ['depth' ] += 1
@@ -49,14 +51,18 @@ def _show_dialog(app_name, create_dialog_func, *args, **kwargs):
49
51
50
52
dlg .show ()
51
53
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.
60
66
if parent is None :
61
67
# We do not use install_except_hook/restore_except_hook so that we can restore the hook actually used when
62
68
# this function is called instead of the one which was used when the module was loaded.
0 commit comments