Is there a way to add a label item that is connected to a handle of a lineROI

67 views
Skip to first unread message

Luka Drmic

unread,
Dec 12, 2018, 10:21:29 AM12/12/18
to pyqtgraph
As the title suggests. Is there a way to add a LabelItem() that is connected  to a Handle() of a lineRoi(). Meaning it follows the handle if the handle is moved.

Thanks.

Patrick

unread,
Dec 12, 2018, 11:00:44 PM12/12/18
to pyqtgraph
Hi,

This isn't very flexible (you'll need to update the list of handles if any are added/removed) but should be a start:

#!/usr/bin/env python3

from PyQt5 import QtWidgets
import pyqtgraph as pg

class TestPlot(pg.GraphicsLayoutWidget):

   
def __init__(self):
       
super().__init__()

       
self.plot = self.addPlot()
       
self.roi = pg.LineSegmentROI(positions=[(0.25, 0.25), (0.75, 0.75)])
       
self.plot.addItem(self.roi, ignoreBounds=True)
       
self.plot.disableAutoRange()
       
self.roi.sigRegionChanged.connect(self._roi_changed)

       
self.roi_labels = [ pg.TextItem(text='label', anchor=(0.5, 1.0)) for _ in self.roi.getHandles() ]
       
for label in self.roi_labels:
           
self.plot.addItem(label)
       
self._roi_changed()

   
def _roi_changed(self):
       
for h_i, handle in enumerate(self.roi.getHandles()):
            handle_pos
= self.roi.pos() + handle.pos()
           
self.roi_labels[h_i].setPos(handle_pos)
           
self.roi_labels[h_i].setText("{}, {}".format(round(handle_pos[0], 2), round(handle_pos[1], 2)))

def main():
   
import sys
    app
= QtWidgets.QApplication(sys.argv)
    mainwindow
= TestPlot()
    mainwindow
.show()
    sys
.exit(app.exec_())

if __name__ == '__main__':
    main
()

Patrick

Luka Drmic

unread,
Dec 13, 2018, 6:50:32 AM12/13/18
to pyqtgraph
I went with something similair.

self.label_a = pg.TextItem("[A]", color=(255, 255, 255))
self.label_a.setAnchor((0, 1))
self.label_a.setParentItem(line_segmet_roi)
self.label_a.setPos(x0, y0)
self.label_b = pg.TextItem("[B]", color=(255, 255, 255))
self.label_b.setAnchor((0, 0))
self.label_a.setParentItem(line_segmet_roi)
self.label_b.setPos(x1, y1)


def update_point_label_positions(self):
    point1
= self.line_segment_roi["ROI"].getSceneHandlePositions(0)
    _
, scene_coords = point1
    coords_a
= self.line_segment_roi["ROI"].mapSceneToParent(scene_coords)
    point2
= self.line_segment_roi["ROI"].getSceneHandlePositions(1)
    _
, scene_coords = point2
    coords_b
= self.line_segment_roi["ROI"].mapSceneToParent(scene_coords)

   
self.label_a.setPos(coords_a.x(), coords_a.y())
   
self.label_b.setPos(coords_b.x(), coords_b.y())

Thx for your reply. Luka.
Reply all
Reply to author
Forward
0 new messages