pyqtgraph: how to drag a a plot item

…衆ロ難τιáo~ 提交于 2019-12-07 01:58:31

Have a look at pyqtgraph/examples/CustomGraphItem.py. The approach there is to create a GraphItem subclass that catches mouse drag events and moves the scatter plot point that is under the mouse:

def mouseDragEvent(self, ev):
    if ev.button() != QtCore.Qt.LeftButton:
        ev.ignore()
        return

    if ev.isStart():
        # We are already one step into the drag.
        # Find the point(s) at the mouse cursor when the button was first 
        # pressed:
        pos = ev.buttonDownPos()
        pts = self.scatter.pointsAt(pos)
        if len(pts) == 0:
            ev.ignore()
            return
        self.dragPoint = pts[0]
        ind = pts[0].data()[0]
        self.dragOffset = self.data['pos'][ind] - pos
    elif ev.isFinish():
        self.dragPoint = None
        return
    else:
        if self.dragPoint is None:
            ev.ignore()
            return

    ind = self.dragPoint.data()[0]
    self.data['pos'][ind] = ev.pos() + self.dragOffset
    self.updateGraph()
    ev.accept()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!