QGraphicsItem 不接收鼠标悬停事件

发布于 2024-09-03 16:54:24 字数 556 浏览 1 评论 0原文

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

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

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

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

仍然没有运气。我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烂人 2024-09-10 16:54:24

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

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

蓝天白云 2024-09-10 16:54:24

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

QGraphicsView::mouseMoveEvent(event);

该调用将事件传播到父级,然后父级将其发送到所有场景项目。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文