pyqtgraph:如何拖动绘图项

时间:2014-03-17 06:34:12

标签: python plot qgraphicsscene pyqtgraph

目前正试图在pyqtgraph中绘制散点图并试图拖动绘图项但无法找到方法。 已经看过GraphicsScene sigMouseClicked,sigMouseMoved事件。 欢迎任何建议。 如果我需要进一步的细节,请告诉我。

我正在使用的示例代码:

import pyqtgraph as pg
import numpy as np

w = pg.GraphicsWindow()
w.show()
x = [2,4,5,6,8];
y = [2,4,6,8,10];

pl = pg.PlotItem()
pl.plot(x, y, symbol='o')
w.addItem(pl)

1 个答案:

答案 0 :(得分:1)

查看pyqtgraph / examples / CustomGraphItem.py。 方法是创建一个GraphItem子类,捕获鼠标拖动事件并移动鼠标下的散点图:

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()