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

Skip to content

Commit 6e63f03

Browse files
committed
moved qApp creation from show() to FigureCanvasQt.__init__()
svn path=/trunk/matplotlib/; revision=2712
1 parent 55044b1 commit 6e63f03

4 files changed

Lines changed: 37 additions & 27 deletions

File tree

CHANGELOG

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
2006-08-27 qt backends: don't create a QApplication when backend is
2-
imported, do it in show(). Simplifies embedding mpl in qt
3-
applications. Updated embedding_in_qt* examples - DSD
2+
imported, do it when the FigureCanvasQt is created. Simplifies
3+
applications where mpl is embedded in qt. Updated
4+
embedding_in_qt* examples - DSD
45

56
2006-08-27 mahtext2.py: Now the fonts are searched in the OS font dir and
67
in the mpl-data dir. Also env is not a dict anymore. - ES

examples/embedding_in_qt4.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ def main():
131131
aw = ApplicationWindow()
132132
aw.setWindowTitle("%s" % progname)
133133
aw.show()
134-
sys.exit(qApp.exec_())
134+
# sys.exit(qApp.exec_())
135+
qApp.exec_()
135136

136137

137138
if __name__ == "__main__": main()

lib/matplotlib/backends/backend_qt.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ def draw_if_interactive():
4040
if figManager != None:
4141
figManager.canvas.draw()
4242

43+
def _create_qApp():
44+
"""
45+
Only one qApp can exist at a time, so check before creating one
46+
"""
47+
if qt.QApplication.startingUp():
48+
if DEBUG: print "Starting up QApplication"
49+
global qApp
50+
qApp = qt.QApplication( [" "] )
51+
qt.QObject.connect( qApp, qt.SIGNAL( "lastWindowClosed()" ),
52+
qApp, qt.SLOT( "quit()" ) )
53+
4354
def show( mainloop=True ):
4455
"""
4556
Show all the figures and enter the qt main loop
@@ -50,30 +61,21 @@ def show( mainloop=True ):
5061

5162
if DEBUG: print 'Inside show'
5263

53-
# We need one and only one QApplication before we can build any Qt widgets
54-
# Detect if a QApplication exists.
55-
createQApp = qt.QApplication.startingUp()
56-
if createQApp:
57-
if DEBUG: print "Starting up QApplication"
58-
qtapplication = qt.QApplication( [" "] )
59-
6064
figManager = Gcf.get_active()
6165
if figManager != None:
6266
figManager.canvas.draw()
6367
#if ( createQApp ):
64-
# qtapplication.setMainWidget( figManager.canvas )
68+
# qApp.setMainWidget( figManager.canvas )
6569

66-
if mainloop and createQApp:
67-
qt.QObject.connect( qtapplication, qt.SIGNAL( "lastWindowClosed()" ),
68-
qtapplication, qt.SLOT( "quit()" ) )
69-
qtapplication.exec_loop()
70+
if mainloop:
71+
qt.qApp.exec_loop()
7072

7173

7274
def new_figure_manager( num, *args, **kwargs ):
7375
"""
7476
Create a new figure manager instance
7577
"""
76-
FigureClass = kwargs.pop('FigureClass', Figure)
78+
FigureClass = kwargs.pop('FigureClass', Figure)
7779
thisFig = FigureClass( *args, **kwargs )
7880
canvas = FigureCanvasQT( thisFig )
7981
manager = FigureManagerQT( canvas, num )
@@ -89,6 +91,8 @@ class FigureCanvasQT( qt.QWidget, FigureCanvasBase ):
8991
buttond = {1:1, 2:3, 4:2}
9092
def __init__( self, figure ):
9193
if DEBUG: print 'FigureCanvasQt: ', figure
94+
_create_qApp()
95+
9296
qt.QWidget.__init__( self, None, "QWidget figure" )
9397
FigureCanvasBase.__init__( self, figure )
9498
self.figure = figure

lib/matplotlib/backends/backend_qt4.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ def draw_if_interactive():
4040
if figManager != None:
4141
figManager.canvas.draw()
4242

43+
def _create_qApp():
44+
"""
45+
Only one qApp can exist at a time, so check before creating one.
46+
"""
47+
if QtGui.QApplication.startingUp():
48+
if DEBUG: print "Starting up QApplication"
49+
global qApp
50+
qApp = QtGui.QApplication( [" "] )
51+
QtCore.QObject.connect( qApp, QtCore.SIGNAL( "lastWindowClosed()" ),
52+
qApp, QtCore.SLOT( "quit()" ) )
53+
4354
def show( mainloop=True ):
4455
"""
4556
Show all the figures and enter the qt main loop
@@ -50,23 +61,14 @@ def show( mainloop=True ):
5061

5162
if DEBUG: print 'Inside show'
5263

53-
# We need one and only one QApplication before we can build any Qt widgets
54-
# Detect if a QApplication exists.
55-
createQApp = QtGui.QApplication.startingUp()
56-
if createQApp:
57-
if DEBUG: print "Starting up QApplication"
58-
qtapplication = QtGui.QApplication( [" "] )
59-
6064
figManager = Gcf.get_active()
6165
if figManager != None:
6266
figManager.canvas.draw()
6367
#if ( createQApp ):
6468
# qtapplication.setMainWidget( figManager.canvas )
6569

66-
if mainloop and createQApp:
67-
QtCore.QObject.connect( qtapplication, QtCore.SIGNAL( "lastWindowClosed()" ),
68-
qtapplication, QtCore.SLOT( "quit()" ) )
69-
qtapplication.exec_()
70+
if mainloop:
71+
qApp.exec_()
7072

7173

7274
def new_figure_manager( num, *args, **kwargs ):
@@ -88,6 +90,8 @@ class FigureCanvasQT( QtGui.QWidget, FigureCanvasBase ):
8890
buttond = {1:1, 2:3, 4:2}
8991
def __init__( self, figure ):
9092
if DEBUG: print 'FigureCanvasQt: ', figure
93+
_create_qApp()
94+
9195
QtGui.QWidget.__init__( self )
9296
FigureCanvasBase.__init__( self, figure )
9397
self.figure = figure

0 commit comments

Comments
 (0)