该代码发生的事情是,当我想使用QGraphicsView :: event()时,GameView :: event()不会捕获QTouchEvent,但是当我不使用QGraphicsView :: event()时一切正常。我想用这个父母 方法,因为我需要处理默认事件。
#include "gameview.h"
#include "QDebug"
#include <QGesture>
#include <QGestureEvent>
#include <QEvent>
#include <QTapGesture>
GameView::GameView(QWidget *parent):QGraphicsView (parent)
{
this->setAttribute(Qt::WA_AcceptTouchEvents);
setAttribute(Qt::WA_StaticContents);
qDebug()<<"I am in GameView constructor\n";
}
GameView::~GameView()
{
qDebug()<<"I am in GameView deconstructor\n";
}
bool GameView::event(QEvent *event)
{
switch (event->type()) {
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
{
QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints();
foreach (const QTouchEvent::TouchPoint &touchPoint, touchPoints)
{
switch (touchPoint.state())
{
case Qt::TouchPointStationary:
// don't do anything if this touch point hasn't moved
continue;
case Qt::TouchPointPressed:
{
qDebug()<<"Touch Pressed";
break;
}
case Qt::TouchPointReleased:
{
qDebug()<<"Touch Release";
break;
}
default:
{
// qDebug()<<"Default";
break;
}
break;
}
}
break;
}
default:
return QGraphicsView::event(event);//if i comment that line then
//its works
}
return true;
}