23

我有一个派生自 的类QGraphicsView,其中包含 -QGraphicsItem派生元素。我希望这些元素在鼠标光标悬停在它们上方时改变颜色,所以我实现了hoverEnterEvent(和hoverLeaveEvent):

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

但是,此事件处理程序代码永远不会执行。我已明确启用鼠标跟踪:

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

尽管如此,还是没有运气。我究竟做错了什么?

4

2 回答 2

33

修复。我需要在我的派生类setAcceptHoverEvents(true)的构造函数中使用。QGraphicsItem

于 2010-05-30T21:39:47.203 回答
5

就我而言,如果我在QGraphicsView类的实现中覆盖了mouseMoveEvent,悬停事件将不起作用。我通过添加调用来解决这个问题

QGraphicsView::mouseMoveEvent(event);

它将事件传播到父级,父级又将其发送到所有场景项目。

于 2016-12-20T22:55:36.420 回答