[Qt-interest] collisions with or picking edges of QGraphicsItem: use createStroke()

lloyd konneker bootch at nc.rr.com
Sat Oct 29 17:15:03 CEST 2011


I found an answer: reimplement QGraphicsItem.shape() and return the path
created by createStroke().  In Python:

def shape(self):
    path = QPainterPath()
    path.addEllipse(self.boundingRect())
    stroke = QPainterPathStroker().createStroke(path)
    return stroke

In other words, a path is ideal, without any thickness.  A stroke is a
path around a path.  A path need not be closed.  A stroke is a closed
path (or set of closed paths.)  A stroke has thickness (the dimension
"across" the two closed paths.)

In the example above, the stroke is a donut: concentric circular paths.
Thus colliding with the stroke of a graphicsItem is colliding with the
edge of the graphics item.

Now a problem is: how to collide in the sense "inside of" and at the
same time collide in the sense "intersects the edge of".  I suppose I
can dynamically alter the shape() method at runtime, between the two
senses.  

But I propose that QGraphicsItem needs an additional method
collidesWithStroke() that calls a virtual method stroke().  In other
words, in the API, expose the idea of the stroked outline of a shape.
Likewise, QGraphicsScene needs strokedItems(point) that returns the
items whose stroke intersects or contains point.




More information about the Qt-interest-old mailing list