QGraphicsItem的hoverMoveEvent()函数以及hover的相关的函数(转载)

QGraphicsItem的hoverMoveEvent()函数

QGraphicsItem的mouseMoveEvent()函数必须鼠标按下的情况下进行move才会触发。
QWidgetmouseMoveEvent()函数可以通过设置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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值