matplotlib and Qt: mouse press event.key is always None

前端 未结 2 536
一向
一向 2020-12-19 10:17

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

相关标签:
2条回答
  • 2020-12-19 10:48

    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:

    class MplWidget(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)
    
    0 讨论(0)
  • 2020-12-19 10:49

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

    0 讨论(0)
提交回复
热议问题