QGraphicsItem不接受鼠标悬停事件 [英] QGraphicsItem doesn't receive mouse hover events
本文介绍了QGraphicsItem不接受鼠标悬停事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类派生自 QGraphicsView
,其中包含 QGraphicsItem
-derived elements。我想让这些元素在鼠标光标悬停在它们上面时改变颜色,所以我实现了 hoverEnterEvent
(和 hoverLeaveEvent
):
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);
...
}
我做错了什么?
推荐答案
修复了它。我需要在我的 QGraphicsItem
-derived类的构造函数中使用 setAcceptHoverEvents(true)
。
Fixed it. I need to use setAcceptHoverEvents(true)
in the constructor of my QGraphicsItem
-derived class.
这篇关于QGraphicsItem不接受鼠标悬停事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文