2

I am trying to embed matplotlib in a Qt interface using Pyside2. I want to use 'imshow' to display an image that I can zoom into using the scrollwheel and click on as well. To connect the scrollwheel and mouse, I am using:

self.fig.canvas.mpl_connect('button_press_event', self.mouseClick)
self.fig.canvas.mpl_connect('scroll_event', self.wheelEvent)

If for the event handlers I use:

def wheelEvent(self, event):
    print(event)

def mouseClick(self, event):
    print(event)

When I run this, and then scroll and click, the output I get is:

button_press_event: xy=(94, 199) xydata=(99.99197230814553, 134.93602887023127) button=1 dblclick=False inaxes=AxesSubplot(0.125,0.290018;0.775x0.409964)
<PySide2.QtGui.QWheelEvent object at 0x7f92e3359a50>

So my question is, why in the one case am I getting a PySide event and in the other a matplotlib button_press_event? What I was expecting was to get a scroll_event.

Many thanks!

1 Answer 1

2

Explanation:

The wheelEvent is a method of the QWidget class that is notified every time it interacts with the wheel when the window has the focus through a QWheelEvent and therefore the classes that inherit from it will also have it, in this case they override that method and you are also connecting it to the scroll_event event so print the QWheelEvent (in my case with PySide2 5.14 on Linux I receive QWheelEvent and scroll_event alternately).

Solution:

Do not use the wheelEvent method but another method with another name:

self.fig.canvas.mpl_connect('button_press_event', self.mouseClick)
self.fig.canvas.mpl_connect('scroll_event', self.mouseWheel)

def mouseWheel(self, event):
    print(event)

def mouseClick(self, event):
    print(event)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that! That would have taken >1e6 years for me to figure out...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.