Issue
I have a plot designed in matplotlib and displayed using QT in python 2.7. I want to connect following two listeners to it:
- button_press_event
- motion_notify_event
Earlier in the past, I was using plain matplotlib and I used to do it in following way:
import matplotlib.pyplot as plt
def mouseClick(event):
pass
def mouseMove(event):
pass
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
fig.canvas.draw()
plt.connect('button_press_event', mouseClick)
plt.connect('motion_notify_event', mouseMove)
plt.show()
Below is my complete QT code:
import sys
from matplotlib.backends import qt_compat
use_pyside = qt_compat.QT_API == qt_compat.QT_API_PYSIDE
if use_pyside:
from PySide import QtGui, QtCore
else:
from PyQt4 import QtGui, QtCore
from numpy import arange, sin, pi
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class MyMplCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
self.axes.plot(t, s)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
class ApplicationWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("application main window")
self.main_widget = QtGui.QWidget(self)
l = QtGui.QVBoxLayout(self.main_widget)
sc = MyMplCanvas(self.main_widget, width=5, height=4, dpi=100)
l.addWidget(sc)
self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
qApp = QtGui.QApplication(sys.argv)
aw = ApplicationWindow()
aw.setWindowTitle("hello")
aw.show()
sys.exit(qApp.exec_())
I want to add listers in the above code. Thanks a lot.
Solution
For this case you must use mpl_connect from FigureCanvas
sc.mpl_connect('button_press_event', self.mouseClick)
sc.mpl_connect('motion_notify_event', self.mouseMove)
Code:
class ApplicationWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("application main window")
self.main_widget = QtGui.QWidget(self)
l = QtGui.QVBoxLayout(self.main_widget)
sc = MyMplCanvas(self.main_widget, width=5, height=4, dpi=100)
l.addWidget(sc)
self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
sc.mpl_connect('button_press_event', self.mouseClick)
sc.mpl_connect('motion_notify_event', self.mouseMove)
def mouseClick(self, event):
print(event)
def mouseMove(self, event):
print(event)
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.