2

I am having trouble capturing the hover enter and hover leave events in a QGraphicsRectItem.

I have subclassed this object, and reimplemented the hover enter and hover leave handlers... or at least I think I have. I also set accepts hover event to true in the constructor.

The event is never fired, however. Breakpoints inside the handlers are never hit.

Here is the class:

#include "qhgraphicsrectitem.h"

QhGraphicsRectItem::QhGraphicsRectItem(QGraphicsRectItem *parent) :
    QGraphicsRectItem(parent)
{
    setAcceptHoverEvents(true);
    setAcceptsHoverEvents(true);
}

void QhGraphicsRectItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
    oldBrush = brush();
    setBrush(QBrush(QColor((oldBrush.color().red() + (0.5 * (255-oldBrush.color().red()))),(oldBrush.color().green() + (0.5 * (255-oldBrush.color().green()))),(oldBrush.color().blue() + (0.5 * (255-oldBrush.color().blue()))))));
}

void QhGraphicsRectItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
    setBrush(oldBrush);
}

What is it that I am doing wrong?

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Luke
  • 2,434
  • 9
  • 39
  • 64
  • The posted code looks good, except for "setAcceptsHoverEvents" which isn't a method of QGraphicsItem. Maybe that's something else you've done. Anyways, you might have to call `update()` after `setBrush`. Are you messing with the rect at all? – Anthony Sep 14 '12 at 17:00
  • @Anthony I'm not changing the rect, no. I'm not handling these events with my QGraphicsView or QGraphicsScene either... I tried update, nothing happened. – Luke Sep 17 '12 at 09:15
  • 1
    "setAcceptsHoverEvent" used to be in the class, they took it out it was depreciated, if you check their sources you'll see. Items don't get hover events because mousemove events dont happen by default when the mouse is up. To get those, override, or use QGraphicsView::setMouseTracking(true) to get the mouse move events to be sent down w/o buttons having to be pressed... i've personally had to update code from setAcceptsHoverEvents to setAcceptHoverEvents, seems like a silly thing to change but i guess it wasn't totally unambiguous so.. – osirisgothra Mar 31 '14 at 19:56
  • Similar question: https://stackoverflow.com/questions/2940392/qgraphicsitem-doesnt-receive-mouse-hover-events . – Gluttton Jun 18 '19 at 19:48

2 Answers2

1

Did you mark your hoverEnterEvent and hoverLeaveEvent as virtual? If you didn't, the events could be triggering but the QGraphicsItem is handling the event instead.

class QhGraphicsRectItem : public QGraphicsItem
{
    ...
    virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
    virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
}
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Keith
  • 225
  • 1
  • 5
1

Is your item located under a QGraphicsItemGroup?

I had this exact same issue until I found this quote:

"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)."

(Look here: http://qt-project.org/doc/qt-4.8/qgraphicsitemgroup.html)

What this means is that QGraphicsItemGroup calls setHandlesChildEvents(true).

I fixed my issue by calling parentItem->setHandlesChildEvents(false) on any (and all) groups located above my item to capture hover events. Poof! The events began showing up in the virtual callbacks you mention.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Lee Dixon
  • 839
  • 1
  • 7
  • 8
  • I had this issue for a different problem, however it doesn't fix my problem that is similar to OP's – Kevin R. Dec 12 '14 at 18:45