Help on PolyLineROI

126 views
Skip to first unread message

Salvatore Lorusso

unread,
Dec 9, 2021, 2:39:16 PM12/9/21
to pyqtgraph
Hi all,

I appreciate if someone has an answer to the following issue.
Please consider the following code:

def roi_changed(roi):
   
    Handles=roi.getLocalHandlePositions() # Get list of handles  
    for h in Handles:
        position=h[1]
        print(position)
        print('try to print X position')
        print(position[0])

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

roi=pg.PolyLineROI([[5.0,2], [6.0, 2], [6.0, 5]], pen=(6,9), closed=False)
plot.addItem(roi)

roi.sigRegionChangeFinished.connect(lambda roi: roi_changed(roi))

print('Print Handles position as they are created')
Handles=roi.getLocalHandlePositions() # Get list of handles
for h in Handles: # Print handles position
    position=h[1]
    print(position)

print('... Then when I move one handle...')

pg.exec()


Initially the code will print the positions of the three handles as following
Point(5.000000, 2.000000)
Point(6.000000, 2.000000)
Point(6.000000, 5.000000)

but when I move one of the handles, so that the "roi_changed" function is activated by the event, the handle that I move changes its "nature" and when I print its position I get:
PyQt5.QtCore.QPointF(6.5, 2.9)
instead of 
Point(6.5, 2.9)

the problem is that I want to extract the X position and while the
position=Point(5,2) is subscriptable, so that position[0]=5
I cannot do the same to extract X position from PyQt5.QtCore.QPointF

In fact you will see that my code fails 

I hope you can help to find a way to extract X position in all the cases

thanks
Salvatore


Ognyan Moore

unread,
Dec 9, 2021, 3:42:05 PM12/9/21
to pyqt...@googlegroups.com
Hi Salvatore,

This one took me a bit to debug!  Pro-tip, if using breakpoint() use the pyside bindings, not the PyQt ones.

Issue is in the ROI.movePoint; or more specifically QGraphicsItem.mapFromScene which takes a pg.Point as input, but returns a QtCore.QPointF; easiest fix would be to modify the method such that `h['pos'] = Point(newPos)` in the movePoint method of the ROI class.

        elif h['type'] == 'f':
            newPos = self.mapFromParent(p1)
            h['item'].setPos(newPos)
            h['pos'] = Point(newPos)
            self.freeHandleMoved = True

At a glance, I'm not sure if there are some other code-paths that would suffer a similar issue.

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/e44e0745-8a65-40f1-b6bb-953234d097d8n%40googlegroups.com.

Salvatore Lorusso

unread,
Dec 10, 2021, 4:28:30 AM12/10/21
to pyqtgraph
Hi Ogi,

if I understand well you need to make some modification to the code.
However I think that in my particular case I can still get rid of the problem using the modification of the code reported below. It seems that it works as well for pg.Point or QtCore.QPointF

def roi_changed(roi):
   
    Handles=roi.getLocalHandlePositions() # Get list of handles  
    for h in Handles:
        position=h[1]
        print(f'point = ({position.x():.4f},{position.y():.4f})')

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

roi=pg.PolyLineROI([[5.0,2], [6.0, 2], [6.0, 5]], pen=(6,9), closed=False)
plot.addItem(roi)

roi.sigRegionChangeFinished.connect(lambda roi: roi_changed(roi))

print('Print Handles position as they are created')
Handles=roi.getLocalHandlePositions() # Get list of handles
for h in Handles: # Print handles position
    position=h[1]
    print(f'point = ({position.x():.4f},{position.y():.4f})')


print('... Then when I move one handle...')

pg.exec()


Do you agree?

thanks 
Salvatore

Ognyan Moore

unread,
Dec 10, 2021, 11:27:53 AM12/10/21
to pyqt...@googlegroups.com
Yeah, the way to do it is not to try and index the object, but to call the .x() and .y() methods.


Reply all
Reply to author
Forward
0 new messages