-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathembeddingInQt4.py
executable file
·76 lines (58 loc) · 1.96 KB
/
embeddingInQt4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
"""
This example illustrates embedding a visvis figure in a Qt application.
This example works for the pyqt4 and pyside backends.
"""
try:
from PySide import QtGui, QtCore # noqa
backend = 'pyside'
except ImportError:
from PyQt4 import QtGui, QtCore # noqa
backend = 'pyqt4'
import visvis as vv
# Create a visvis app instance, which wraps a qt4 application object.
# This needs to be done *before* instantiating the main window.
app = vv.use(backend)
class MainWindow(QtGui.QWidget):
def __init__(self, *args):
QtGui.QWidget.__init__(self, *args)
# Make a panel with a button
self.panel = QtGui.QWidget(self)
but = QtGui.QPushButton(self.panel)
but.setText('Push me')
# Make figure using "self" as a parent
Figure = app.GetFigureClass()
self.fig = Figure(self)
# Make sizer and embed stuff
self.sizer = QtGui.QHBoxLayout(self)
self.sizer.addWidget(self.panel, 1)
self.sizer.addWidget(self.fig._widget, 2)
# Make callback
but.pressed.connect(self._Plot)
# Apply sizers
self.setLayout(self.sizer)
# Finish
self.resize(560, 420)
self.setWindowTitle('Embedding in Qt (%s)' % backend)
self.show()
def _Plot(self):
# Make sure our figure is the active one.
# If only one figure, this is not necessary.
#vv.figure(self.fig.nr)
# Clear it
vv.clf()
# Plot
vv.plot([1,2,3,1,6])
vv.legend(['this is a line'])
#self.fig.DrawNow()
# Two ways to create the application and start the main loop
if True:
# The visvis way. Will run in interactive mode when used in IEP or IPython.
app.Create()
m = MainWindow()
app.Run()
else:
# The native way.
qtApp = QtGui.QApplication([''])
m = MainWindow()
qtApp.exec_()