matplotlib 和 Qt:鼠标按下 event.key 总是无 [英] matplotlib and Qt: mouse press event.key is always None

查看:80
本文介绍了matplotlib 和 Qt:鼠标按下 event.key 总是无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Qt (PySide) 应用程序中嵌入了一个 matplotlib 图,我想结合修饰键(例如 shift 和 control)响应鼠标点击事件.在下面的代码中,我使用 canvas mpl_connect 方法将button_press_event"连接到打印 event.key 值的虚拟处理程序.即使我在鼠标单击期间按下修饰键,该值也始终为无".在 Qt 应用程序中嵌入 matplotlib 图时,如何获取 matplotlib 鼠标事件处理程序中修饰键的状态?

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 code below I used the canvas mpl_connect method to connect 'button_press_event' to a dummy handler that prints the value of event.key. This value is always 'None' even if I press a modifier key during the mouse click. How can I get the state of modifier keys in a matplotlib mouse event handler when embedding a matplotlib figure in a Qt application?

谢谢.

import sys

from PySide import QtGui, QtCore

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

class MplCanvas(FigureCanvas):
    """Class to represent the FigureCanvas widget"""
    def __init__(self):
        self.fig = Figure()        
        self.ax = self.fig.add_subplot(111)

        FigureCanvas.__init__(self, self.fig)
        FigureCanvas.setSizePolicy(self, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

        self.fig.canvas.mpl_connect('button_press_event',self._on_press)

    def _on_press(self,event):
        print( event.key )


class MplWidget(QtGui.QWidget):
    """Widget defined in Qt Designer"""
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)

        self.canvas = MplCanvas()

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

        self.setLayout(self.vbl)


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.mpl = MplWidget(self.centralwidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.mpl.sizePolicy().hasHeightForWidth())
        self.mpl.setSizePolicy(sizePolicy)
        self.mpl.setObjectName("mpl")
        self.verticalLayout.addWidget(self.mpl)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))

class DesignerMainWindow(QtGui.QMainWindow, Ui_MainWindow):
    """Customization for Qt Designer created window"""
    def __init__(self, parent = None):
        super(DesignerMainWindow, self).__init__(parent)
        # setup the GUI --> function generated by pyuic4
        self.setupUi(self)


if __name__ == '__main__':

    # create the GUI application
    app = QtGui.QApplication(sys.argv)
    # instantiate the main window
    dmw = DesignerMainWindow()
    # show it
    dmw.show()
    # start the Qt main loop execution, exiting from this script
    # with the same return code of Qt application
    sys.exit(app.exec_())

推荐答案

解决方案,由 OP 提供,编辑出答案:

我刚刚在此处找到了我的问题的答案.
问题是一般不会处理按键事件,除非您将 qt 的焦点激活到 mpl 画布上".解决方法是在 MplWidget 类中添加两行:

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)

这篇关于matplotlib 和 Qt:鼠标按下 event.key 总是无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆