QGraphicsView和eventFilter [英] QGraphicsView and eventFilter

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

问题描述

这已经困扰了我超过两天,所以我想我应该问.我正在Win7上使用Qt 4.5.3(与VC2008一起编译).

This has been bugging me for more than two days now, so i thought i should ask. I am using Qt 4.5.3 (compiled with VC2008) on Win7.

我有MyGraphicsView(继承QGraphicsView)和MyFilter(继承QObject)类.

I have MyGraphicsView (inherits QGraphicsView) and MyFilter (inherits QObject) classes.

当我将MyFilter对象作为事件过滤器安装到MyGraphicsView时,鼠标事件会在 之后传递到MyFilter ,而键事件会在之前传递到MyFiltericsView.它们被传送到MyGraphicsView.

When i install the MyFilter object as an event filter to MyGraphicsView, Mouse events are delivered to MyFilter after they are delivered to MyGraphicsView whereas Key events are delivered to MyFilter before they are delivered to MyGraphicsView.

在第二种情况下,我将MyFilter对象作为事件过滤器安装到MyGraphicsView-> viewport()(这是标准的QGLWidget),鼠标事件在传递到MyFilter之前先传递给MyFilter.MyGraphicsView,而关键事件仅传递给 MyGraphicsView.

In the second case, i install the MyFilter object as an event filter to MyGraphicsView->viewport() (which is a standart QGLWidget), Mouse events are delivered to MyFilter before they are delivered to MyGraphicsView, whereas Key events are delivered to only MyGraphicsView.

应该将事件先传递给事件过滤器,然后再传递给实际对象,为什么会这样呢?我该怎么做才能确保此订单?

The events are supposed to be delivered to event filters before they are delivered to the actual object, so why is this happening? What should i do to ensure this order?

先谢谢了.此致.

推荐答案

QGraphicsView是QAbstractScrollArea的子类,是这些行为的原因.

QGraphicsView is a subclass of QAbstractScrollArea which is the cause of these behaviors.

在第一种情况下,当调用setViewport()时,QAbstractScrollArea将自身作为事件过滤器添加到MyGraphicsView中.QAbstractScrollArea的事件过滤器捕获鼠标事件,首先通过viewportEvent()将其发送,然后传递给QWidget事件处理,该事件再传播到MyGraphicsView鼠标事件处理程序.只有在此之后,QAbstractScrollArea的事件过滤器才会完成,MyFilter才能运行.

In the first case, the QAbstractScrollArea adds itself as a event filter to the MyGraphicsView when setViewport() is called. The QAbstractScrollArea's event filter captures the mouse event, first sends it through viewportEvent(), and then to the QWidget event handling which propagates to the MyGraphicsView mouse event handlers. Only after this is the QAbstractScrollArea's event filter finished and MyFilter gets to run.

在第二种情况下,键事件仅传递给MyGraphicsView,因为QAbstractScrollArea在setViewport()中将自身设置为焦点代理.如果使用以下代码重置了焦点代理,则将传递关键事件.

In the second case, key events are delivered only to the MyGraphicsView because in setViewport() the QAbstractScrollArea sets itself as the focus proxy. If the focus proxy is reset with the following code, the key events will be delivered.

w.viewport()->setFocusProxy(0);

另一种方法是在图形视图及其视口上都安装事件过滤器,但将过滤器修改为仅处理来自一个对象的键事件和来自另一个对象的鼠标事件.

An alternative is to install the event filter on both the graphics view and its viewport, but modify the filter to only process key events from one object and mouse events from the other.

更改MyFilter.h

Change MyFilter.h

  QObject *keyObj;
  QObject *mouseObj;

public:
  MyFilter(QObject *keyObj, QObject *mouseObj, QObject *parent = NULL);

更改MyFilter.cpp

Change MyFilter.cpp

MyFilter::MyFilter(QObject *keyObj, QObject *mouseObj, QObject *parent /*= NULL*/ ) : QObject(parent), keyObj(keyObj), mouseObj(mouseObj)

if (obj == keyObj && e->type() == QEvent::KeyPress)
{
    qDebug()<<"Key Event recieved by MyFilter";
}
else if (obj == mouseObj && e->type() == QEvent::MouseButtonPress)
{
    qDebug()<<"Mouse Event recieved by MyFilter";
}

更改main.cpp

Change main.cpp

MyFilter *filter = new MyFilter(&w, w.viewport(), &w);

// Use this line to install to the viewport
w.viewport()->installEventFilter(filter);

//Use this line to install to MyGraphicsView
w.installEventFilter(filter);

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

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