使用悬停事件
Using Hover Events
我在捕获QGraphicsRectItem
中的悬停进入和悬停离开事件时遇到问题。
我已经子类化了这个对象,并重新实现了悬停输入和悬停离开处理程序......或者至少我认为我有。我还在构造函数中将接受悬停事件设置为 true。
但是,该事件永远不会触发。永远不会命中处理程序内的断点。
这是类:
#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
正在处理事件。
class QhGraphicsRectItem : public QGraphicsItem
{
...
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
}
您的物品是否位于QGraphicsItemGroup
下?
在我找到这句话之前,我遇到了完全相同的问题:
"QGraphicsItemGroup
是一种特殊类型的复合项目,它将自身及其所有子项视为一个项目(即,所有子项的所有事件和几何图形都合并在一起)。
(看这里: http://qt-project.org/doc/qt-4.8/qgraphicsitemgroup.html)
这意味着QGraphicsItemGroup
调用setHandlesChildEvents(true)
。
通过在项目上方的任何(和所有)组上调用parentItem->setHandlesChildEvents(false)
来捕获悬停事件,从而解决了我的问题。 噗! 事件开始显示在您提到的虚拟回调中。
相关文章:
- Android NDK传感器向事件队列报告奇怪的间隔
- 从文本文件中读取时钟时间和事件时间并进行处理
- WMI检测进程创建事件-c++
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 处理闪烁窗口事件
- C++Builder中的OnClick事件签名存在问题
- 将鼠标悬停在问题上时与预期">"相关的代码错误
- 跟踪滚动条上的鼠标事件
- 什么是事件表 (wxWidgets)?
- 如何在 MFCaptureEngine 中获取"Camera removed"事件
- 给定顺序中的事件处理
- 当服务中的事件被触发时,如何将响应从服务发送回客户端?
- 在 C++/CLI 中将 .NET 事件从一个 DLL 引发到另一个 DLL
- 如何创建事件驱动的 SDL2 应用程序
- Windows 进程间同步类似事件?
- 如何捕获 QSystemTrayIcon 的鼠标悬停事件
- 使用悬停事件
- 如何处理特定控件的鼠标悬停事件在Microsoft visual c++ (MFC)
- 重叠的QGraphicsItem-s悬停事件
- 如何捕获Windows窗体按钮的鼠标悬停事件