How to recognize the ROI I moved

140 views
Skip to first unread message

Salvatore Lorusso

unread,
Dec 6, 2021, 2:17:06 PM12/6/21
to pyqtgraph
Hi all,

I am a beginner, so I hope not to post too trivial question, but I am trying to understand how to do the following: assume I have defined two ROIs and I set the event to print "Hi" when I finish to move a ROI.
I am able to print "Hi" when I move ANY of the two ROIs, but how can recognize WHICH ROI I moved? Like printing "Hi I moved ROI N. 1" or "Hi I moved ROI N. 2" ?

Here a simple code to explain better my question:

import pyqtgraph as pg

app = pg.mkQApp("Examples")
win = pg.GraphicsLayoutWidget(show=True, size=(800,800), border=True)
win.setWindowTitle('ROI Example')

plot = win.addPlot()
plot.setXRange(0,10)
plot.setYRange(0,10)

rois=[]
rois.append(pg.ROI(pos=[3,5],size=[1,1]))
rois.append(pg.ROI(pos=[7,5],size=[1,1]))

for roi in rois:
    plot.addItem(roi)
    roi.sigRegionChangeFinished.connect(lambda: print('Hi'))

pg.exec()

Thanks in advance for any help
Salvatore

Patrick

unread,
Dec 6, 2021, 9:24:53 PM12/6/21
to pyqtgraph
Hi,

I'm pretty sure when the signals are emitted, they also pass a reference to the ROI which triggered the signal. It's not clear from the documentation, but can see it in the underlying code for the ROI (https://pyqtgraph.readthedocs.io/en/latest/_modules/pyqtgraph/graphicsItems/ROI.html#ROI.stateChanged). So try changing the connect() line to something like:

roi.sigRegionChangeFinished.connect(lambda r: print(r.pos()))

and see if that works.

Patrick

Salvatore Lorusso

unread,
Dec 7, 2021, 4:11:51 AM12/7/21
to pyqtgraph
Hi Patrick and all,

Thanks for the quick answer!

Your modification works, but still it does not answer my doubt: I understood that the function that I pass to "connect" assumes to have as argument the ROI itself, but what you suggest give me back the position of the ROI that I moved... true, however I don't know yet WHICH I moved. Having the print of the position, it simply tells me that ONE of them (but not WHICH of them) has reached that position.
Is there a way to give some "attribute" to the ROI, like a name or ID, so that the ROI that I moved can pass me back such identifier?

Thanks
Salvatore

Patrick

unread,
Dec 7, 2021, 4:30:05 AM12/7/21
to pyqtgraph
Hi,

No problem. You just need to compare the objects to see if they refer to the same instance. So the simplest is something like:
if r == rois[0]:
   print("First ROI")

A better example is:

import pyqtgraph as pg

app = pg.mkQApp("Examples")
win = pg.GraphicsLayoutWidget(show=True, size=(800,800), border=True)
win.setWindowTitle('ROI Example')

plot = win.addPlot()
plot.setXRange(0,10)
plot.setYRange(0,10)

rois=[]
rois.append(pg.ROI(pos=[3,5],size=[1,1]))
rois.append(pg.ROI(pos=[7,5],size=[1,1]))

def roi_changed(roi):
    global rois
    try:
        roi_i = rois.index(roi)
        print(f"ROI #{roi_i} at {roi.pos()}")
    except:
        print("Unknown ROI!")


for roi in rois:
    plot.addItem(roi)
    roi.sigRegionChangeFinished.connect(roi_changed)

pg.exec()

Salvatore Lorusso

unread,
Dec 7, 2021, 6:25:38 AM12/7/21
to pyqtgraph
Excellent solution Patrick!

Thanks a lot
Salvatore

Reply all
Reply to author
Forward
0 new messages