Question

I have QPainterPath. I need to find y coordinate of QPainterPath by x.

I found intersected() method in QPainterPath. So, I created new QPainterPath, which is line from left to right edge of my path's bounding rect with x coordinate, to find point as result of intersection.

intersects() method returns true. But intersected() returns empty path.

Everything works If I use rect with height = 1 instead of line.

Maybe you have a better idea how to find intersection of QPainterPath with line?

Was it helpful?

Solution

According to the documentation:

QPainterPath QPainterPath::intersected ( const QPainterPath & p ) const

Returns a path which is the intersection of this path's fill area and p's fill area. Bezier curves may be flattened to line segments due to numerical instability of doing bezier curve intersections.

Since your line has no fill area, it would seem like this function would not work for you.

If you are using a QGraphicsScene to display your QPainterPath you can use the method collidingItems:

QList QGraphicsScene::collidingItems ( const QGraphicsItem * item, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const

Returns a list of all items that collide with item. Collisions are determined by calling QGraphicsItem::collidesWithItem(); the collision detection is determined by mode. By default, all items whose shape intersects item or is contained inside item's shape are returned. The items are returned in descending stacking order (i.e., the first item in the list is the uppermost item, and the last item is the lowermost item).

Unfortunately, QPainter does not seem to have the same function. I think that your method of creating a long rectangle might be an easier way of doing this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top