Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7147317

Browse files
committed
Merge pull request #1705 from tacaswell/qt_closeevent_v1.2
Qt closeevent fixes for v1.2.x
2 parents 2dc2825 + 671e0ec commit 7147317

File tree

3 files changed

+67
-24
lines changed

3 files changed

+67
-24
lines changed

lib/matplotlib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@ def tk_window_focus():
11221122
'matplotlib.tests.test_triangulation',
11231123
'matplotlib.tests.test_transforms',
11241124
'matplotlib.tests.test_arrow_patches',
1125+
'matplotlib.tests.test_backend_qt4',
11251126
]
11261127

11271128

lib/matplotlib/backends/backend_qt4.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,14 @@ def idle_draw(*args):
367367
self._idle = True
368368
if d: QtCore.QTimer.singleShot(0, idle_draw)
369369

370+
370371
class MainWindow(QtGui.QMainWindow):
371372
def closeEvent(self, event):
372373
self.emit(QtCore.SIGNAL('closing()'))
374+
QtGui.QMainWindow.closeEvent(self, event)
375+
373376

374-
class FigureManagerQT( FigureManagerBase ):
377+
class FigureManagerQT(FigureManagerBase):
375378
"""
376379
Public attributes
377380
@@ -381,29 +384,31 @@ class FigureManagerQT( FigureManagerBase ):
381384
window : The qt.QMainWindow
382385
"""
383386

384-
def __init__( self, canvas, num ):
385-
if DEBUG: print('FigureManagerQT.%s' % fn_name())
386-
FigureManagerBase.__init__( self, canvas, num )
387+
def __init__(self, canvas, num):
388+
if DEBUG:
389+
print('FigureManagerQT.%s' % fn_name())
390+
FigureManagerBase.__init__(self, canvas, num)
387391
self.canvas = canvas
388392
self.window = MainWindow()
389393
self.window.connect(self.window, QtCore.SIGNAL('closing()'),
390-
canvas.close_event)
394+
canvas.close_event)
395+
self.window.connect(self.window, QtCore.SIGNAL('closing()'),
396+
self._widgetclosed)
391397

392398
self.window.setWindowTitle("Figure %d" % num)
393-
image = os.path.join( matplotlib.rcParams['datapath'],'images','matplotlib.png' )
394-
self.window.setWindowIcon(QtGui.QIcon( image ))
399+
image = os.path.join(matplotlib.rcParams['datapath'], 'images', 'matplotlib.png')
400+
self.window.setWindowIcon(QtGui.QIcon(image))
395401

396402
# Give the keyboard focus to the figure instead of the
397403
# manager; StrongFocus accepts both tab and click to focus and
398404
# will enable the canvas to process event w/o clicking.
399405
# ClickFocus only takes the focus is the window has been
400406
# clicked
401-
# on. http://developer.qt.nokia.com/doc/qt-4.8/qt.html#FocusPolicy-enum
402-
self.canvas.setFocusPolicy( QtCore.Qt.StrongFocus )
407+
# on. http://qt-project.org/doc/qt-4.8/qt.html#FocusPolicy-enum or
408+
# http://doc.qt.digia.com/qt/qt.html#FocusPolicy-enum
409+
self.canvas.setFocusPolicy(QtCore.Qt.StrongFocus)
403410
self.canvas.setFocus()
404411

405-
QtCore.QObject.connect( self.window, QtCore.SIGNAL( 'destroyed()' ),
406-
self._widgetclosed )
407412
self.window._destroying = False
408413

409414
self.toolbar = self._get_toolbar(self.canvas, self.window)
@@ -419,7 +424,7 @@ def __init__( self, canvas, num ):
419424
# requested size:
420425
cs = canvas.sizeHint()
421426
sbs = self.window.statusBar().sizeHint()
422-
self._status_and_tool_height = tbs_height+sbs.height()
427+
self._status_and_tool_height = tbs_height + sbs.height()
423428
height = cs.height() + self._status_and_tool_height
424429
self.window.resize(cs.width(), height)
425430

@@ -428,14 +433,14 @@ def __init__( self, canvas, num ):
428433
if matplotlib.is_interactive():
429434
self.window.show()
430435

431-
def notify_axes_change( fig ):
436+
def notify_axes_change(fig):
432437
# This will be called whenever the current axes is changed
433438
if self.toolbar is not None:
434439
self.toolbar.update()
435-
self.canvas.figure.add_axobserver( notify_axes_change )
440+
self.canvas.figure.add_axobserver(notify_axes_change)
436441

437442
@QtCore.Slot()
438-
def _show_message(self,s):
443+
def _show_message(self, s):
439444
# Fixes a PySide segfault.
440445
self.window.statusBar().showMessage(s)
441446

@@ -445,8 +450,9 @@ def full_screen_toggle(self):
445450
else:
446451
self.window.showFullScreen()
447452

448-
def _widgetclosed( self ):
449-
if self.window._destroying: return
453+
def _widgetclosed(self):
454+
if self.window._destroying:
455+
return
450456
self.window._destroying = True
451457
try:
452458
Gcf.destroy(self.num)
@@ -474,15 +480,19 @@ def resize(self, width, height):
474480
def show(self):
475481
self.window.show()
476482

477-
def destroy( self, *args ):
483+
def destroy(self, *args):
478484
# check for qApp first, as PySide deletes it in its atexit handler
479-
if QtGui.QApplication.instance() is None: return
480-
if self.window._destroying: return
485+
if QtGui.QApplication.instance() is None:
486+
return
487+
if self.window._destroying:
488+
return
481489
self.window._destroying = True
482-
QtCore.QObject.disconnect( self.window, QtCore.SIGNAL( 'destroyed()' ),
483-
self._widgetclosed )
484-
if self.toolbar: self.toolbar.destroy()
485-
if DEBUG: print("destroy figure manager")
490+
QtCore.QObject.disconnect(self.window, QtCore.SIGNAL('destroyed()'),
491+
self._widgetclosed)
492+
if self.toolbar:
493+
self.toolbar.destroy()
494+
if DEBUG:
495+
print("destroy figure manager")
486496
self.window.close()
487497

488498
def get_window_title(self):
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from matplotlib import pyplot as plt
2+
from matplotlib.testing.decorators import cleanup
3+
from matplotlib.testing.decorators import knownfailureif
4+
from matplotlib._pylab_helpers import Gcf
5+
import copy
6+
7+
try:
8+
import matplotlib.backends.qt4_compat
9+
HAS_QT = True
10+
except ImportError:
11+
HAS_QT = False
12+
13+
14+
@cleanup
15+
@knownfailureif(not HAS_QT)
16+
def test_fig_close():
17+
# force switch to the Qt4 backend
18+
plt.switch_backend('Qt4Agg')
19+
20+
#save the state of Gcf.figs
21+
init_figs = copy.copy(Gcf.figs)
22+
23+
# make a figure using pyplot interface
24+
fig = plt.figure()
25+
26+
# simulate user clicking the close button by reaching in
27+
# and calling close on the underlying Qt object
28+
fig.canvas.manager.window.close()
29+
30+
# assert that we have removed the reference to the FigureManager
31+
# that got added by plt.figure()
32+
assert(init_figs == Gcf.figs)

0 commit comments

Comments
 (0)