From c4299c46374995f1dbe31faa4b2c1b175a880829 Mon Sep 17 00:00:00 2001 From: tonysyu Date: Tue, 25 Jun 2013 14:37:01 -0500 Subject: [PATCH] Fix execution in IPython with qt backend. New QApplication and event-loop implementation stolen shamelessly from IPython. Strangely, running the viewer at the IPython prompt will open an orphan Matplotlib figure window, but running a script using `%run` does not. Only tested on PySide (not PyQt4). --- skimage/viewer/utils/core.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/skimage/viewer/utils/core.py b/skimage/viewer/utils/core.py index 826147eb..4ec3e1cf 100644 --- a/skimage/viewer/utils/core.py +++ b/skimage/viewer/utils/core.py @@ -22,22 +22,37 @@ __all__ = ['init_qtapp', 'start_qtapp', 'RequiredAttr', 'figimage', 'update_axes_image'] -QApp = None - - def init_qtapp(): """Initialize QAppliction. The QApplication needs to be initialized before creating any QWidgets """ - global QApp - if QApp is None: - QApp = QtGui.QApplication([]) + app = QtGui.QApplication.instance() + if app is None: + app = QtGui.QApplication([]) + return app -def start_qtapp(): +def is_event_loop_running(app=None): + """Return True if event loop is running.""" + if app is None: + app = init_qtapp() + if hasattr(app, '_in_event_loop'): + return app._in_event_loop + else: + return False + + +def start_qtapp(app=None): """Start Qt mainloop""" - QApp.exec_() + if app is None: + app = init_qtapp() + if not is_event_loop_running(app): + app._in_event_loop = True + app.exec_() + app._in_event_loop = False + else: + app._in_event_loop = True class RequiredAttr(object):