Important: Please read the Qt Code of Conduct -

[SOLVED] hover{Enter/Move/Leave}Event() events called only when QGraphicsItems is clicked

  • Hi all,

    I think the topic title explains most of the problem. I am trying to catch the hoverEnterEvent() on a QGraphicsItem using a pretty simple event handler:

    @void NodeItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)

    qDebug() << "Mouse entering NodeItem";


    However, the message appears only if I click the Item. Same happens with hoverMove and hoverLeave events. Why is that?

    Edit: I did set the setAcceptHoverEvents(true) in my constructor.

  • The only thing that worked for me was setting the protected function to virtual:

    //hover events
    virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
    virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
    virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);@

  • Thank you for your anwser. Unfortunately, they already are since I always declare overriden functions as virtual for documentation purposes, although it's not strictly necessary. It still doesn't work...

  • The only other thing I can think of would be setting a flag. For my objects I have:
    setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemSendsScenePositionChanges | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);

    But I wouldn't think that any of those enable hovering.

  • Moderators

    are you sure you are not filtering the events to the graphics-view's viewport?

  • I honestly don't know how to? What should I look for?

  • I just noticed that the boundingRect() and shape() functions are not implemented for my NodeItem class. Could it cause a problem in the hovering zone detection?

  • Yes. Qt uses the boundingRect() function to pass events to objects, based on their location.

  • Just tried it. It did not work :(
    It still only works when I click on the item.

  • Hmm. I would say look at the colliding mice example (Qt\Version\examples\graphicsview\collidingmice), and see if you can implement something close to that. The colliding mice example doesn't currently have hover events, but you could implement them.

  • Tried it... and it works perfectly for the mice! I am clueless about what I am doing wrong here.

    Could the fact that my NodeItem subclasses the QGraphicsProxyWidget class instead of QGraphicsItem cause a problem? The NodeItem is also composed of a few child QWidgets and QGraphicsItem.

    EDIT: I also subclass the QGraphicsView and QGraphicsScene classes to implement custom behavior. Should I do anything with the event()/eventFilter() functions?

    MORE EDITS: Don't think the event()/eventFilter() functions have anything to do with it, as I just ran some experiments on the more complex Diagramscene example and it works! :'(

  • Urrggghhhh! Found it. Problem was event propagation from the GraphicsView.

    The line

    was at the wrong place.

Log in to reply