Skip to content

Commit 892c715

Browse files
committed
Merge pull request #894 from hmeine/qt4_ignore_extra_mouse_buttons
let Qt4 backend ignore extra mouse buttons (fixes exceptions)
2 parents 50381be + 518b9a9 commit 892c715

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

lib/matplotlib/backends/backend_qt4.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,13 @@ class FigureCanvasQT( QtGui.QWidget, FigureCanvasBase ):
122122
QtCore.Qt.Key_Alt : 'alt',
123123
QtCore.Qt.Key_Return : 'enter'
124124
}
125-
# left 1, middle 2, right 3
126-
buttond = {1:1, 2:3, 4:2}
125+
# map Qt button codes to MouseEvent's ones:
126+
buttond = {QtCore.Qt.LeftButton : 1,
127+
QtCore.Qt.MidButton : 2,
128+
QtCore.Qt.RightButton : 3,
129+
# QtCore.Qt.XButton1 : None,
130+
# QtCore.Qt.XButton2 : None,
131+
}
127132
def __init__( self, figure ):
128133
if DEBUG: print 'FigureCanvasQt: ', figure
129134
_create_qApp()
@@ -165,9 +170,10 @@ def mousePressEvent( self, event ):
165170
x = event.pos().x()
166171
# flipy so y=0 is bottom of canvas
167172
y = self.figure.bbox.height - event.pos().y()
168-
button = self.buttond[event.button()]
169-
FigureCanvasBase.button_press_event( self, x, y, button )
170-
if DEBUG: print 'button pressed:', event.button()
173+
button = self.buttond.get(event.button())
174+
if button is not None: # only three buttons supported by MouseEvent
175+
FigureCanvasBase.button_press_event( self, x, y, button )
176+
if DEBUG: print('button pressed:', event.button())
171177

172178
def mouseMoveEvent( self, event ):
173179
x = event.x()
@@ -180,9 +186,10 @@ def mouseReleaseEvent( self, event ):
180186
x = event.x()
181187
# flipy so y=0 is bottom of canvas
182188
y = self.figure.bbox.height - event.y()
183-
button = self.buttond[event.button()]
184-
FigureCanvasBase.button_release_event( self, x, y, button )
185-
if DEBUG: print 'button released'
189+
button = self.buttond.get(event.button())
190+
if button is not None: # only three buttons supported by MouseEvent
191+
FigureCanvasBase.button_release_event( self, x, y, button )
192+
if DEBUG: print('button released')
186193

187194
def wheelEvent( self, event ):
188195
x = event.x()

0 commit comments

Comments
 (0)