使用Hover事件 [英] Using Hover Events

查看:1513
本文介绍了使用Hover事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在捕获 QGraphicsRectItem 中的悬停进入和悬停事件时遇到问题。

I am having trouble capturing the hover enter and hover leave events in a QGraphicsRectItem.

我已子类化这个对象,并重新实现了悬停进入和悬停离开处理程序...或至少我认为我有。我还在构造函数中设置接受hover事件为true。

I have subclassed this object, and reimplemented the hover enter and hover leave handlers... or at least I think I have. I also set accepts hover event to true in the constructor.

但是,事件从未被触发。

The event is never fired, however. Breakpoints inside the handlers are never hit.

以下是类:

#include "qhgraphicsrectitem.h"

QhGraphicsRectItem::QhGraphicsRectItem(QGraphicsRectItem *parent) :
    QGraphicsRectItem(parent)
{
    setAcceptHoverEvents(true);
    setAcceptsHoverEvents(true);
}

void QhGraphicsRectItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
    oldBrush = brush();
    setBrush(QBrush(QColor((oldBrush.color().red() + (0.5 * (255-oldBrush.color().red()))),(oldBrush.color().green() + (0.5 * (255-oldBrush.color().green()))),(oldBrush.color().blue() + (0.5 * (255-oldBrush.color().blue()))))));
}

void QhGraphicsRectItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
    setBrush(oldBrush);
}

我做错了什么?

推荐答案

您标记了 hoverEnterEvent hoverLeaveEvent 为虚拟?如果没有,事件可能触发,但 QGraphicsItem 正在处理事件。

Did you mark your hoverEnterEvent and hoverLeaveEvent as virtual? If you didn't, the events could be triggering but the QGraphicsItem is handling the event instead.

class QhGraphicsRectItem : public QGraphicsItem
{
    ...
    virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
    virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
}

这篇关于使用Hover事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆