开发者

QGraphicsItem doesn't receive mouse hover events

I have a class derived from QGraphicsView, which contains QGraphicsItem-derived elements. I want these elements to change color whenever the mouse cursor hovers over them, so I implemented hoverEnterEvent (and hoverLeaveEvent):

void MyGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
    update (boundingRect());
}

However, this event handler code is never executed. I've 开发者_运维知识库explicitly enabled mouse tracking:

MyGraphicsView::MyGraphicsView(MainView *parent) :
    QGraphicsView(parent)
{
    setMouseTracking(true);
    viewport()->setMouseTracking(true);
    ...
}

Still, no luck. What am I doing wrong?


Fixed it. I need to use setAcceptHoverEvents(true) in the constructor of my QGraphicsItem-derived class.


In my case, hover events wouldn't work if I overrode mouseMoveEvent in my implementation of the QGraphicsView class. I fixed this by adding a call to

QGraphicsView::mouseMoveEvent(event);

which propagated the event to the parent, which in turn sent it out to all the scene items.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜