QGraphicsItem的hoverMoveEvent()函数
QGraphicsItem的mouseMoveEvent()函数必须鼠标按下的情况下进行move才会触发。
QWidget的mouseMoveEvent()函数可以通过设置setMouseTracking(true)在鼠标不需要按下的情况下,触发鼠标move事件。
QGraphicsItem要实现鼠标不按下的情况下,获取鼠标move事件,可通过**hoverMoveEvent()来实现,可通过设置setAcceptHoverEvents(true)**使其生效
底下是摘自另一篇关于hover的文章,版权同样归原创作者所有,如有侵权请联系及时删除: http://amin-ahmadi.com/2016/02/01/mouse-hover-over-event-qt-widget/
Qt does not have mouse hover, or mouse over (as some people call it) events in its widgets by default but it provides the means to do it quite easily if you just know how to inherit a class and add some protected functions to it. Here is an example that shows how to create a push button (QPushButton) that reacts to mouse hover events.
First, you have to create a class that inherits QPushButton and add the following functions to its protected section of methods and properties:
protected:
void hoverEnter(QHoverEvent *event);
void hoverLeave(QHoverEvent *event);
void hoverMove(QHoverEvent *event);
bool event(QEvent *event);
Next step, you should manually catch QEvent::HoverEnter, QEvent::HoverLeave and QEvent::HoverMove events in the event function and return the rest:
switch (event->type())
{
case QEvent::HoverEnter:
hoverEnter(static_cast<QHoverEvent*>(event));
return true;
break;
case QEvent::HoverLeave:
hoverLeave(static_cast<QHoverEvent*>(event));
return true;
break;
case QEvent::HoverMove:
hoverMove(static_cast<QHoverEvent*>(event));
return true;
break;
default:
break;
}
return QWidget::event(event);
Now you can just simply write the contents of what you want to do in the body of hoverEnter, hoverLeave and hoverMove functions.
Download the following complete and simple project that fully demonstrates what is described in this article.
Hover Sensitive Button