Search code examples
pythonpyqtgraphroi

ROI handles dont change their position while/after dragging


heyho

situation

i have multiple scatterplots in a viewbox in pyqtgraph. since i want to 'cut something out' of it, i create a PolyLineROI somewhere in the plot with initial three handles. when i start dragging the whole thing to its destination (and append maybe append another handle) the positions of the handles still have their original position from where it started. even when i drag the handles themselves they change their values relativ to their starting position... which results in the wrong selected area.

MWE

import pyqtgraph as pg

def roiMove():
    """Print the coordinates of the ROI."""
    print(roi.getState()['points'])

win = pg.GraphicsWindow()
vb = win.addViewBox()
# roi in rectangle shape
roi = pg.PolyLineROI([[1, 2], [1.5, 2], [1, 0]],
    closed=True,
    pen=pg.mkPen(color=(0, 201, 255, 255), width=3)
    )

# connect to printing function when moving something
# NOTE: when dragging the whole ROI the positions won't be updated
roi.sigRegionChanged.connect(roiMove)
vb.addItem(roi)
pg.QtGui.QApplication.exec_()

it would be awesome if someone had an idea :-)

cheers


Solution

  • I got the answer in the real world... instead of the state's points it was the SceneHandlePositions that needed be mapped to the parent:

    MWE

    import pyqtgraph as pg
    
    def roiMove():
        """Print the coordinates of the ROI."""
        pts = roi.getSceneHandlePositions()
        print([roi.mapSceneToParent(pt[1]) for pt in pts])
    
    win = pg.GraphicsWindow()
    vb = win.addViewBox()
    # roi in triangle shape
    roi = pg.PolyLineROI([[1, 2], [1.5, 2], [1, 0]],
                closed=True,
                pen=pg.mkPen(color=(0, 201, 255, 255), width=3)
                    )
    
    # connect to printing function when moving something
    roi.sigRegionChanged.connect(roiMove)
    vb.addItem(roi)
    vb.setXRange(-5, 5)
    vb.setYRange(-5, 5)
    pg.QtGui.QApplication.exec_()