Skip to content Skip to sidebar Skip to footer

Matplotlib And Qt: Mouse Press Event.key Is Always None

I embedded a matplotlib figure in a Qt (PySide) application and I would like to respond to mouse click events in combination with modifier keys (e.g. shift and control). In the cod

Solution 1:

Solution, provided by OP, edited out of the answer:

I just found the answer to my question here. The issue is that key press events in general are not processed unless you "activate the focus of qt onto your mpl canvas". The solution is to add two lines to the MplWidget class:

classMplWidget(QtGui.QWidget):
    """Widget defined in Qt Designer"""def__init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)

        self.canvas = MplCanvas()

        #THESE TWO LINES WERE ADDED
        self.canvas.setFocusPolicy( QtCore.Qt.ClickFocus )
        self.canvas.setFocus()

        self.vbl = QtGui.QVBoxLayout()
        self.vbl.addWidget(self.canvas)

        self.setLayout(self.vbl)

Solution 2:

event.key is used when connecting key_press_event, here you actually may wanna check event.button==1 (or 2, etc.)

Post a Comment for "Matplotlib And Qt: Mouse Press Event.key Is Always None"