5

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?

Mat
  • 202,337
  • 40
  • 393
  • 406
Funt
  • 399
  • 8
  • 23
  • I've created a suggestion for similar functionality to be added: https://bugreports.qt-project.org/browse/QTBUG-32123 – Mitch Jul 09 '13 at 14:43
  • The correct link is: https://bugreports.qt.io/browse/QTBUG-32313 – Mitch Apr 26 '15 at 05:30

1 Answers1

2

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.

buster
  • 1,038
  • 7
  • 16
  • Thanks for the answer. Unfortunately, I don't use QGraphicsScene. And I found a case when intersected() method causes stack overflow :( So it seems that I just need to find QPainterPath::Element with y, which is closest to what I need. – Funt Feb 22 '12 at 13:17
  • You should probably switch to `QGraphicsScene`, it is best for handling multiple items. Otherwise create a `QList` and keep track of every path you throw on the `QPainter`, then you can just iterate the list and search for a path that has the appropriate x coordinate. – buster Feb 22 '12 at 15:08