8000 added embedding in qt example · matplotlib/matplotlib@28641bc · GitHub
[go: up one dir, main page]

Skip to content

Commit 28641bc

Browse files
committed
added embedding in qt example
svn path=/trunk/matplotlib/; revision=1236
1 parent e72d352 commit 28641bc

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
New entries should be added at the top
22

3+
2005-04-26 Added embedding in qt example.
34

45
2005-04-14 Applied Michael Brady's qt backend patch: 1) fix a bug
56
where keyboard input was grabbed by the figure and not

examples/embedding_in_qt.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#! /usr/bin/env python
2+
# Copyright (C) 2005 Florent Rougon
3+
#
4+
# This file is an example program for matplotlib. It may be used,
5+
# distributed and modified without limitation.
6+
import sys, os, random
7+
from qt import *
8+
9+
from matplotlib.numerix import arange, sin, pi
10+
11+
# The QApplication has to be created before backend_qt is imported, otherwise
12+
# it will create one itself.
13+
QApplication.setColorSpec(QApplication.NormalColor)
14+
app = QApplication(sys.argv)
15+
16+
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas
17+
from matplotlib.figure import Figure
18+
19+
TRUE = 1
20+
FALSE = 0
21+
22+
progname = os.path.basename(sys.argv[0])
23+
progversion = "0.1"
24+
25+
26+
class FloMplCanvas(FigureCanvas):
27+
"""Ultimately, this is a qWidget (as well as a FigureCanvasAgg, etc.)."""
28+
def __init__(self, parent=None, width=5, height=4, dpi=100):
29+
self.fig = Figure(figsize=(width,height), dpi=dpi)
30+
self.axes = self.fig.add_subplot(111)
31+
self.compute_initial_figure()
32+
33+
FigureCanvas.__init__(self, self.fig)
34+
self.reparent(parent, QPoint(0, 0))
35+
36+
FigureCanvas.setSizePolicy(self,
37+
QSizePolicy.Expanding,
38+
QSizePolicy.Expanding)
39+
FigureCanvas.updateGeometry(self)
40+
41+
def sizeHint(self):
42+
w, h = self.fig.get_width_height()
43+
return QSize(w, h)
44+
45+
def minimumSizeHint(self):
46+
return QSize(10, 10)
47+
48+
49+
class FloCanvas1(FloMplCanvas):
50+
def compute_initial_figure(self):
51+
t = arange(0.0, 3.0, 0.01)
52+
s = sin(2*pi*t)
53+
self.axes.plot(t, s)
54+
55+
class FloCanvas2(FloMplCanvas):
56+
def __init__(self, *args, **kwargs):
57+
FloMplCanvas.__init__(self, *args, **kwargs)
58+
timer = QTimer(self, "Canvas update timer")
59+
QObject.connect(timer, SIGNAL("timeout()"), self.update_figure)
60+
timer.start(1000, FALSE)
61+
62+
def compute_initial_figure(self):
63+
self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r')
64+
65+
def update_figure(self):
66+
l = []
67+
for i in range(4):
68+
l.append(random.randint(0, 10))
69+
70+
self.axes.lines = []
71+
self.axes.plot([0, 1, 2, 3], l, 'r')
72+
self.draw()
73+
74+
75+
class ApplicationWindow(QMainWindow):
76+
def __init__(self):
77+
QMainWindow.__init__(self, None,
78+
"application main window",
79+
Qt.WType_TopLevel | Qt.WDestructiveClose)
80+
81+
self.file_menu = QPopupMenu(self)
82+
self.file_menu.insertItem('&Quit', self.fileQuit, Qt.CTRL + Qt.Key_Q)
83+
self.menuBar().insertItem('&File', self.file_menu)
84+
85+
# self.file_menu.insertSeparator()
86+
87+
self.help_menu = QPopupMenu(self)
88+
self.menuBar().insertSeparator()
89+
self.menuBar().insertItem('&Help', self.help_menu)
90+
91+
self.help_menu.insertItem('&About', self.about)
92+
93+
self.main_widget = QWidget(self, "Main widget")
94+
95+
l = QVBoxLayout(self.main_widget)
96+
c1 = FloCanvas1(self.main_widget, width=5, height=4, dpi=100)
97+
c2 = FloCanvas2(self.main_widget, width=5, height=4, dpi=100)
98+
l.addWidget(c1)
99+
l.addWidget(c2)
100+
101+
self.main_widget.setFocus()
102+
self.setCentralWidget(self.main_widget)
103+
104+
self.statusBar().message("Ready", 2000)
105+
106+
def fileQuit(self):
107+
qApp.exit(0)
108+
109+
def closeEvent(self, ce):
110+
self.fileQuit()
111+
112+
def about(self):
113+
QMessageBox.about(self, "About %s" % progname,
114+
"""%(prog)s version %(version)s
115+
Copyright (c) 2005 Florent Rougon
116+
117+
This program is a monitor for the Conrad Charge Manager 2010
118+
""" % {"prog": progname, "version": progversion})
119+
120+
121+
def main():
122+
mw = ApplicationWindow()
123+
mw.setCaption("%s" % progname)
124+
qApp.setMainWidget(mw)
125+
mw.show()
126+
sys.exit(app.exec_loop())
127+
128+
129+
if __name__ == "__main__": main()

0 commit comments

Comments
 (0)
0