PDA

View Full Version : QGraphicsPathItem changing state when hovered



Vit Stepanek
8th December 2010, 12:32
Hi,
I have a "chart" view using QGraphicsView. Each line of data consists of few QGraphicsPathItems wrapped by a QGraphicsItemGroup. My intent is to highlight a data line when hovered by a mouse.

Here comes the problem: The hover events on the group are sent when I hover the rect of the line, but not the line itself (which is a curve). What's more, when more data lines go over each other, only the "top" one receives the hover event (due to z order).
I could implement a function which would iterate over points of the data lines, but don't want to reinvent a wheel in case I can do it simpler.

So, does anybody know a way how to detect that a cursor is above the curve itself, not only the rectangular area it's bounded by?

Thanks
Vit

totem
8th December 2010, 13:08
Did you subclass QGraphicsPathItem class ?
If so, can you post the ::shape() method you defined for the subclass ?

This is the method used by hover events to determine if you are "on" an item

Vit Stepanek
8th December 2010, 14:15
Nope, I'll try it. Now I think if the problem can't be caused by handling the event in the QGraphicsItemGroup object instead of the PathItem itself? I should know in few minutes...

Added after 36 minutes:

I have subclassed the QGraphicsPathItem class and implemented the hover event handlers as following:



void hoverEnterEvent( QGraphicsSceneHoverEvent * )
{
m_isMouseOver = true;
dbg( QString( "Hovered a path %1" ).arg( (int)this ) );
}

void hoverLeaveEvent( QGraphicsSceneHoverEvent * )
{
m_isMouseOver = false;
}



I also call setAcceptHoverEvents(true) from the ctor. But the event handler is never reached. Could be there a problem with the fact that it's a curve, so it has no area?

totem
8th December 2010, 16:17
I did not read before, but documentation states that "A QGraphicsItemGroup is a special type of compound item that treats itself and all its children as one item (i.e., all events and geometries for all children are merged together)"

Is it mandatory to group all paths in a QGraphicsItemGroup ?

Vit Stepanek
8th December 2010, 16:44
Well, that's a question. I use my class that's derived from QGraphicsItemGroup to fill it with measured data, each represents some measured feature in the time (x axis). However, the new data can be added to the existing displayed line.
But using some merging functionality I could avoid having the QGraphicsItemGroup used. Let me try.

Thanks