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

Skip to content

Commit 55044b1

Browse files
committed
For qt backends, dont create a QApplication when the backend is
imported. Do it in show(). svn path=/trunk/matplotlib/; revision=2711
1 parent 89a69dc commit 55044b1

5 files changed

Lines changed: 32 additions & 39 deletions

File tree

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
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
4+
15
2006-08-27 mahtext2.py: Now the fonts are searched in the OS font dir and
26
in the mpl-data dir. Also env is not a dict anymore. - ES
37

examples/embedding_in_qt.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@
1212
from qt import *
1313

1414
from matplotlib.numerix import arange, sin, pi
15-
16-
# The QApplication has to be created before backend_qt is imported, otherwise
17-
# it will create one itself.
18-
# Note: color-intensive applications may require a different color allocation
19-
# strategy.
20-
QApplication.setColorSpec(QApplication.NormalColor)
21-
app = QApplication(sys.argv)
22-
2315
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas
2416
from matplotlib.figure import Figure
2517

@@ -31,6 +23,10 @@
3123
progname = os.path.basename(sys.argv[0])
3224
progversion = "0.1"
3325

26+
# Note: color-intensive applications may require a different color allocation
27+
# strategy.
28+
QApplication.setColorSpec(QApplication.NormalColor)
29+
app = QApplication(sys.argv)
3430

3531
class MyMplCanvas(FigureCanvas):
3632
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""

examples/embedding_in_qt4.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,9 @@
1313
from PyQt4 import QtGui, QtCore
1414

1515
from matplotlib.numerix import arange, sin, pi
16-
17-
# The QApplication has to be created before backend_qt is imported, otherwise
18-
# it will create one itself.
19-
# Note: color-intensive applications may require a different color allocation
20-
# strategy.
21-
QtGui.QApplication.setColorSpec(QtGui.QApplication.NormalColor)
22-
qApp = QtGui.QApplication(sys.argv)
23-
2416
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
2517
from matplotlib.figure import Figure
2618

27-
# This seems to be what PyQt expects, according to the examples shipped in
28-
# its distribution.
29-
TRUE = 1
30-
FALSE = 0
31-
3219
progname = os.path.basename(sys.argv[0])
3320
progversion = "0.1"
3421

@@ -44,7 +31,7 @@ def __init__(self, parent=None, width=5, height=4, dpi=100):
4431
self.compute_initial_figure()
4532

4633
FigureCanvas.__init__(self, self.fig)
47-
self.setParent(parent)#, QtCore.QPoint(0, 0))
34+
self.setParent(parent)
4835

4936
FigureCanvas.setSizePolicy(self,
5037
QtGui.QSizePolicy.Expanding,
@@ -117,7 +104,7 @@ def __init__(self):
117104
self.statusBar().showMessage("All hail matplotlib!", 2000)
118105

119106
def fileQuit(self):
120-
qApp.exit(0)
107+
self.close()
121108

122109
def closeEvent(self, ce):
123110
self.fileQuit()
@@ -132,13 +119,17 @@ def about(self):
132119
133120
It may be used and modified with no restriction; raw copies as well as
134121
modified versions may be distributed without limitation."""
135-
% {"prog": progname, "version": progversion})
122+
% {"prog": progname, "version": progversion})
136123

137124

138125
def main():
126+
# Note: color-intensive applications may require a different color
127+
# allocation strategy.
128+
QtGui.QApplication.setColorSpec(QtGui.QApplication.NormalColor)
129+
qApp = QtGui.QApplication(sys.argv)
130+
139131
aw = ApplicationWindow()
140132
aw.setWindowTitle("%s" % progname)
141-
## qApp.setMainWidget(aw)
142133
aw.show()
143134
sys.exit(qApp.exec_())
144135

lib/matplotlib/backends/backend_qt.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ def show( mainloop=True ):
4949
manager.window.show()
5050

5151
if DEBUG: print 'Inside show'
52+
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+
5260
figManager = Gcf.get_active()
5361
if figManager != None:
5462
figManager.canvas.draw()
@@ -397,10 +405,3 @@ def exception_handler( type, value, tb ):
397405

398406

399407
FigureManager = FigureManagerQT
400-
401-
# We need one and only one QApplication before we can build any Qt widgets
402-
# Detect if a QApplication exists.
403-
createQApp = qt.QApplication.startingUp()
404-
if createQApp:
405-
if DEBUG: print "Starting up QApplication"
406-
qtapplication = qt.QApplication( [" "] )

lib/matplotlib/backends/backend_qt4.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ def show( mainloop=True ):
4949
manager.window.show()
5050

5151
if DEBUG: print 'Inside show'
52+
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+
5260
figManager = Gcf.get_active()
5361
if figManager != None:
5462
figManager.canvas.draw()
@@ -367,10 +375,3 @@ def exception_handler( type, value, tb ):
367375

368376

369377
FigureManager = FigureManagerQT
370-
371-
# We need one and only one QApplication before we can build any Qt widgets
372-
# Detect if a QApplication exists.
373-
createQApp = QtGui.QApplication.startingUp()
374-
if createQApp:
375-
if DEBUG: print "Starting up QApplication"
376-
qtapplication = QtGui.QApplication( [" "] )

0 commit comments

Comments
 (0)