source: bcdaqwidgets/trunk/src/bcdaqwidgets/embedding_in_qt4.py @ 1425

Last change on this file since 1425 was 1425, checked in by jemian, 10 years ago

try to build GUI from .ui files

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Date Revision Author HeadURL Id
File size: 4.0 KB
Line 
1#!/usr/bin/env python
2
3# embedding_in_qt4.py --- Simple Qt4 application embedding matplotlib canvases
4#
5# Copyright (C) 2005 Florent Rougon
6#               2006 Darren Dale
7#
8# This file is an example program for matplotlib. It may be used and
9# modified with no restriction; raw copies as well as modified versions
10# may be distributed without limitation.
11
12from __future__ import unicode_literals
13import sys, os, random
14from PySide import QtGui, QtCore
15
16import matplotlib
17matplotlib.rcParams['backend.qt4']='PySide'
18
19from numpy import arange, sin, pi
20from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
21from matplotlib.figure import Figure
22
23progname = os.path.basename(sys.argv[0])
24progversion = "0.1"
25
26
27class MyMplCanvas(FigureCanvas):
28    """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
29    def __init__(self, parent=None, width=5, height=4, dpi=100):
30        fig = Figure(figsize=(width, height), dpi=dpi)
31        self.axes = fig.add_subplot(111)
32        # We want the axes cleared every time plot() is called
33        self.axes.hold(False)
34
35        self.compute_initial_figure()
36
37        #
38        FigureCanvas.__init__(self, fig)
39        self.setParent(parent)
40
41        FigureCanvas.setSizePolicy(self,
42                                   QtGui.QSizePolicy.Expanding,
43                                   QtGui.QSizePolicy.Expanding)
44        FigureCanvas.updateGeometry(self)
45
46    def compute_initial_figure(self):
47        pass
48
49
50class MyStaticMplCanvas(MyMplCanvas):
51    """Simple canvas with a sine plot."""
52    def compute_initial_figure(self):
53        t = arange(0.0, 3.0, 0.01)
54        s = sin(2*pi*t)
55        self.axes.plot(t, s)
56
57
58class MyDynamicMplCanvas(MyMplCanvas):
59    """A canvas that updates itself every second with a new plot."""
60    def __init__(self, *args, **kwargs):
61        MyMplCanvas.__init__(self, *args, **kwargs)
62        timer = QtCore.QTimer(self)
63        QtCore.QObject.connect(timer, QtCore.SIGNAL("timeout()"), self.update_figure)
64        timer.start(1000)
65
66    def compute_initial_figure(self):
67         self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r')
68
69    def update_figure(self):
70        # Build a list of 4 random integers between 0 and 10 (both inclusive)
71        l = [ random.randint(0, 10) for i in range(4) ]
72
73        self.axes.plot([0, 1, 2, 3], l, 'r')
74        self.draw()
75
76
77class ApplicationWindow(QtGui.QMainWindow):
78    def __init__(self):
79        QtGui.QMainWindow.__init__(self)
80        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
81        self.setWindowTitle("application main window")
82
83        self.file_menu = QtGui.QMenu('&File', self)
84        self.file_menu.addAction('&Quit', self.fileQuit,
85                                 QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
86        self.menuBar().addMenu(self.file_menu)
87
88        self.help_menu = QtGui.QMenu('&Help', self)
89        self.menuBar().addSeparator()
90        self.menuBar().addMenu(self.help_menu)
91
92        self.help_menu.addAction('&About', self.about)
93
94        self.main_widget = QtGui.QWidget(self)
95
96        l = QtGui.QVBoxLayout(self.main_widget)
97        sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100)
98        dc = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100)
99        l.addWidget(sc)
100        l.addWidget(dc)
101
102        self.main_widget.setFocus()
103        self.setCentralWidget(self.main_widget)
104
105        self.statusBar().showMessage("All hail matplotlib!", 2000)
106
107    def fileQuit(self):
108        self.close()
109
110    def closeEvent(self, ce):
111        self.fileQuit()
112
113    def about(self):
114        QtGui.QMessageBox.about(self, "About",
115"""embedding_in_qt4.py example
116Copyright 2005 Florent Rougon, 2006 Darren Dale
117
118This program is a simple example of a Qt4 application embedding matplotlib
119canvases.
120
121It may be used and modified with no restriction; raw copies as well as
122modified versions may be distributed without limitation."""
123)
124
125
126qApp = QtGui.QApplication(sys.argv)
127
128aw = ApplicationWindow()
129aw.setWindowTitle("%s" % progname)
130aw.show()
131sys.exit(qApp.exec_())
132#qApp.exec_()
Note: See TracBrowser for help on using the repository browser.