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

Skip to content

Commit bbd5f4c

Browse files
committed
Merge pull request #4312 from jenshnielsen/qtfixes
DOC : Some fixes to qt 4 and 5 examples
2 parents 1b11d9d + 49fbc0a commit bbd5f4c

File tree

3 files changed

+145
-2
lines changed

3 files changed

+145
-2
lines changed

examples/user_interfaces/embedding_in_qt4.py

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

lib/matplotlib/backends/backend_qt5.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ def __init__(self, figure):
234234
# NB: Using super for this call to avoid a TypeError:
235235
# __init__() takes exactly 2 arguments (1 given) on QWidget
236236
# PyQt5
237+
# The need for this change is documented here
238+
# http://pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html#cooperative-multi-inheritance
237239
super(FigureCanvasQT, self).__init__(figure=figure)
238240
self.figure = figure
239241
self.setMouseTracking(True)

0 commit comments

Comments
 (0)