事件过滤器QGraphicsItem [英] Event filter on QGraphicsItem
问题描述
是否可以在 QGraphicsItem
上添加事件过滤器? Eventfilter有一个参数,它给你一个 QObject
,但由于 QGraphicsItem
不是从
Is it possible to have an event filter on a QGraphicsItem
? Eventfilter has a param that gives you a QObject
, but since QGraphicsItem
isn't derived from QObject
, then how would it work?
推荐答案
编辑: QGraphicsItem :: installSceneEventFilter ,如@ Frank的回答中所建议。示例:
Use QGraphicsItem::installSceneEventFilter as suggested in @Frank's answer. Example:
QGraphicsScene scene;
QGraphicsEllipseItem *ellipse = scene.addEllipse(QRectF(-10, -10, 20, 20));
QGraphicsLineItem *line = scene.addLine(QLineF(-10, -10, 20, 20));
line->installSceneEventFilter(ellipse);
// line's events are filtered by ellipse's sceneEventFilter() function.
ellipse->installSceneEventFilter(line);
// ellipse's events are filtered by line's sceneEventFilter() function.
:
The first thing that popped into my mind was this:
创建一个新类,源自 QGraphicsItem
和 QObject
,因为这些都是不相关的(只要看一下docs告诉我),你应该有你想要的。
Create a new class, derived from both QGraphicsItem
and QObject
, since these are unrelated (as far as a glance at the docs tells me), you should have what you wanted.
....但是后来我更仔细看了文档,发现QGraphicsObject,这可能是你想要的,它甚至已经有成员 eventFilter
.... But then I looked at the docs more closely and found QGraphicsObject, which is probably exactly what you want, it even already has the member eventFilter
这篇关于事件过滤器QGraphicsItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!