PDA

View Full Version : hoverLeaveEvent ??



gattogio
16th February 2007, 14:39
Hi

i have a class that inherits QGraphicsItem that reimplements the hoverLeaveEvent method. Every instance of this class could have several children (that also inherit from QGraphicsItem and reimplement hoverLeaveEvent), that are painted into the item area. Everytime the mouse moves over one of these children, the hoverLeaveEvent of the parent is called, while I don't want this to happen (or I need a way to ignore it). How can I do this?

P.S.
every item involved invokes the method setAcceptsHoverEvents(true)

Gopala Krishna
16th February 2007, 15:20
Try this

//In constructor of parent item class
ParentItem::ParentItem()
{
...
setHandlesChildEvents(false);
}

EDIT: Sorry misread the post. Have you reimplemented hoverEnterEvent() ?

gattogio
16th February 2007, 15:38
Yes, I reimplemented hoverEnterEvent and hoverLeaveEvent for all the classes involved.

Calling setHandlesChildEvent() don't give better results...

wysota
16th February 2007, 15:44
Test the cursor position in the event whether it actually is inside the item's shape. Remember about mapping coordinates from screen to item.

gattogio
16th February 2007, 16:31
Isn't there a more elegant solution?

My actual implementation looks like this:



void ParentClass::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ) {

qreal x = event->pos().x();
qreal y = event->pos().y();

if ( x <= boundingRect().x()
x >= boundingRect().right()
y <= boundingRect().y()
y >= pos.y() >= boundingRect().bottom() ){

doSomething();
}
}


An alternative to the preceding should be like this:



void ParentClass::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ) {
QPointF tmpPos = event->pos();
if ( !boundingRect().contains( tmpPos ) )
doSomething()
}


but the result isn't the same... anyone can explain me why????

wysota
16th February 2007, 21:48
You should test against the shape, not the bounding rectangle.

How are the results different?

gattogio
19th February 2007, 09:15
I test the bounding rectangle because my shape is compliant to the bounding rectangle. :)
The responses between the two code frames above are different because th e second one doesn't care about the edge of the rectangle.

I suspect a bug in the Qt Libraries, the method description is:

bool QRectF::contains ( const QPointF & point ) const
Returns true if the given point is inside or on the edge of the rectangle; otherwise returns false.

wysota
19th February 2007, 11:50
These are floating point calculations. You can be suffering from approximations.

I just looked at the sources. The method is correct. It does the same thing as your first method (just normalises the rectangle first).