Important: Please read the Qt Code of Conduct -

QPainterPath selection problem

  • Hi! I am trying to plot a line plot in QWidget or in QGraphicsView. I am using QPainterPath to draw my line plot in both, and I am trying to have an ability to select the given Path by either clicking on it or highlight the path when the mouse hovers over it. I am doing this in QWidget using bool QPainterPath::intersects(const QRectF &rectangle) const function and in QGraphicsView using void QGraphicsScene::focusItemChanged() Signal. With QWidget I can detect the mouse hover and with QGraphicsView I can detect mouse press.

    I only want to detect a mouse hover or mouse pressed when my cursor is right over the QPainterPath but it is being triggered even when the cursor is in the red region in the below image (the black thick line is my QPainterPath).


    It appears that this is happening because Qt considers the area enclosed by the path as being part of QPainterPath. I also checked by drawing my line plot with QPolygonF, it has the same result as QPainterPath. (I do not understand Qt's thinking in this regard, an open path is a group of small line segments joined together end-to-end, not an area). Is there any way around it? All I want to do is to find out when my mouse is over my path.

  • Lifetime Qt Champion


    Why not use QGraphicsItem::hoverEnterEvent and its friend ?

  • @SGaist I tried using that but it requires the calculation of boundingRect() beforehand and implementing it in QGraphicsScene by calling prepareGeometryChange() to allow QGraphicsScene to update its bookkeeping. This will result in an extra calculation burden which I am trying to avoid, I will use it if I have no other option.
    I tried to solve this problem by using a "kind of" boundary for my curve by drawing curves above and below my actual curve and to detect QPainterPath::intersects() for both of them. This would give me an XOR logic to detect the mouse hover event. So my curves look like this:


    With green one being the lower curve and red one being the upper curve. But when I draw the QPainterPath the enclosed area becomes something like this:


    Where red is the enclosed area of the upper curve and green is the enclosed area of the lower curve. These enclosed areas make no sense to me, what kind of a mathematical formula will result in 3 sine waves varying just in their base amplitude (i.e. only their zeros lie at different y values) will result in an enclosed area like that.

Log in to reply