如何获取 pyqtgraph 小部件上的鼠标光标坐标?

每当我双击小部件时,我想获取 pyqtgraph 小部件上的鼠标光标坐标并将它们发送到函数。我查找了如何做并可以找到一些类似的问题,但我仍然无法获得正确的坐标。


import pyqtgraph as pg

from pyqtgraph.Qt import QtGui, QtCore

from pyqtgraph.dockarea import *

from PyQt5.QtGui import *

from PyQt5.QAxContainer import *

from PyQt5.QtCore import *


def on_double_click_out(event):

    pos = event

    print('clicked', pos)

        

class Plotter():

    def __init__(self):

        pg.setConfigOption('background', 'w')

        pg.setConfigOption('foreground', 'k')


        self.win = QtGui.QMainWindow()

        self.area = DockArea()

        self.win.setCentralWidget(self.area)

        self.win.resize(1000,500)

        self.win.setWindowTitle('pyqtgraph example: dockarea')

 

        self.dock_TR = Dock("TR", size=(300,10))  

        self.dock_TR.hideTitleBar()

        

        self.area.addDock(self.dock_TR) 

        

        self.widgetTR = pg.PlotWidget()

        self.widgetTR.showGrid(x=True, y=True, alpha=0.3)   

        self.dock_TR.addWidget(self.widgetTR)        

        self.win.show()        

            

p = Plotter()

proxy = pg.SignalProxy(p.widgetTR.scene().sigMouseClicked, rateLimit=60, slot=on_double_click_out)

当我在 widgetTR 上单击(不是双击)时,我得到一些坐标,但它们与 x、y 轴无关。例如,当我单击小部件的 (1, 1) 时,结果是clicked (<MouseClickEvent (573,-259) button=1>,)我还想知道是否可以通过双击来完成此操作。


德玛西亚99
浏览 191回答 1
1回答

慕码人8056858

您可以使用 pyqtgraph.GraphicsScene.mouseEvents.MouseClickEvent.double() 来查看 MouseClickEvent 是否是双击。您确定坐标不正确吗?坐标系从左上角的 (0,0) 开始。试试这个代码,它对我有用:import pyqtgraph as pgfrom pyqtgraph.Qt import QtGui, QtCorefrom pyqtgraph.dockarea import *from PyQt5.QtGui import *from PyQt5.QtCore import *from PyQt5 import QtCore, QtGui, QtWidgets, uicimport numpy as npdef on_double_click_out(event):&nbsp; &nbsp; mouseEvent = event[0]&nbsp; &nbsp; mousePoint = mouseEvent.pos()&nbsp; &nbsp; if mouseEvent.double():&nbsp; &nbsp; &nbsp; &nbsp; print("Double click")&nbsp; &nbsp; if p.p1.sceneBoundingRect().contains(mousePoint):&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; print('x=', mousePoint.x(), ' y=', mousePoint.y())&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;class Plotter():&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; pg.setConfigOption('background', 'w')&nbsp; &nbsp; &nbsp; &nbsp; pg.setConfigOption('foreground', 'k')&nbsp; &nbsp; &nbsp; &nbsp; self.win = pg.GraphicsLayoutWidget(show=True)&nbsp; &nbsp; &nbsp; &nbsp; self.win.resize(1000,500)&nbsp; &nbsp; &nbsp; &nbsp; self.win.setWindowTitle('pyqtgraph example: dockarea')&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; self.p1 = self.win.addPlot()&nbsp; &nbsp; &nbsp; &nbsp; self.win.show()&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;p = Plotter()proxy = pg.SignalProxy(p.win.scene().sigMouseClicked, rateLimit=60, slot=on_double_click_out)if __name__ == '__main__':&nbsp; &nbsp; import sys&nbsp; &nbsp; if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):&nbsp; &nbsp; &nbsp; &nbsp; QtGui.QApplication.instance().exec_()您还可以查看 pyqtgraph 示例“crosshair.py”。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python