2

I need to get the vertices of a QPolygonF. This is my code till now

class Example(QtGui.QGraphicsScene):
    def block(self): 
        self.bpoint1 = QtCore.QPointF(150 , 150)
        self.bpoint2 = QtCore.QPointF(200 , 150)
        self.bpoint3 = QtCore.QPointF(200 , 200)
        self.bpoint4 = QtCore.QPointF(150 , 200)
        self.bproto = QtGui.QPolygonF([self.bpoint1 , self.bpoint2 , self.bpoint3 , self.bpoint4])
        self.block = QtGui.QGraphicsPolygonItem()
        self.block.setPolygon(self.bproto)
        self.block.setFlag(QtGui.QGraphicsItem.ItemIsMovable)

My block becomes now movable , and I need to get the new vertices every time , my block is moved.How do I do it? I tried self.block.boundingRect() , but it gives me the same RectF . Could some-one help me out.

Manoj
  • 961
  • 4
  • 11
  • 37

1 Answers1

3

QPolygon just derives from QVector, so just use the normal array accessors. But they will be in local coordinates, so they need to be mapped to scene coordinates (I assume that's you mean by 'the new vertices') - use block.mapToScene( block.polygon()[i] ).

cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • What exactly is block here? because I;m getting an attribute error. Can't look at the docs also , because Im getting a 404 error. – Manoj Jan 28 '13 at 03:44
  • What exactly is block here? because I;m getting an attribute error. Can't look at the docs also , because Im getting a 404 error. If I assume its self.block , wher block is my QGraphicsPolygonItem and if I do for i in range(3): self.block.mapToScene(self.block.polygon()[i]) and I still print self.block.boundingRect() , it still gives me a constant value. @cmannett85 – Manoj Jan 28 '13 at 03:57
  • Yes, I meant `self.block`. I think you have gotten confused here, `boundingRect()` returns the bounding rectangle in _local_ coordinates. `self.block.mapToScene(self.block.polygon()[i])` _returns_ the ith point in the polygon transformed by the item's inverse transformation matrix, thereby pushing the coordinate into scene coordinates. – cmannett85 Jan 28 '13 at 08:07
  • Also, if you are just creating a rectangle, use `QGraphicsRectItem` as it's optimised for that. – cmannett85 Jan 28 '13 at 08:08