
Originally Posted by
Oliver Knoll
Thanks, I will stich your example together and try it out on Mac OS X and post my results here.
Unfortunatelly it really seems that on Mac (MacBook Pro with touchpad, Mac OS 10.6.6) the QGraphicsObject based items do not receive Gesture events 
I took your example and adapted it slightly:
- use qDebug instead of stdout, so I actually see the debug statements also in Qt Creator
- explicitly also grab gestures for the viewport! You mentioned that in your posts, but I don't see that in your actual example code (probably it is there in your real code, as you say "Within the GraphicsView", and in the example code there is no derived class of QGraphicsView)
Anyway, just for the record:
TouchObject.h:
#ifndef ___TOUCHOBJECT___
#define ___TOUCHOBJECT___
#include <QtGui>
#include <QGesture>
#include <iostream>
class TouchObject : public QGraphicsObject {
Q_OBJECT
public:
TouchObject
(QGraphicsItem* pParent
= NULL) : QGraphicsObject
(pParent
) {
grabGesture(Qt::PinchGesture);
setAcceptTouchEvents(true);
}
return QRectF(-WIDTH
/ 2,
-HEIGHT
/ 2, WIDTH, HEIGHT
);
}
path.addEllipse(boundingRect());
return path;
}
Q_UNUSED(pOption)
Q_UNUSED(pWidget)
painter->setBrush(Qt::blue);
painter->drawEllipse(boundingRect());
}
bool sceneEvent
(QEvent* pEvent
) { QGestureEvent* pGestureEvent ;
switch (pEvent->type()) {
pEvent->accept();
qDebug("Touch Event Received from sceneEvent()");
return true;
qDebug("Gesture Event Received from sceneEvent()");
pGestureEvent = static_cast<QGestureEvent*>(pEvent);
if(QGesture *pinch = pGestureEvent->gesture(Qt::PinchGesture)) {
qDebug(" Pinch Gesture: state: %d", pinch->state());
}
else {
qDebug(" not a pinch gesture.");
}
break;
default:
break;
}
return QGraphicsObject::sceneEvent(pEvent);
}
private:
static const int WIDTH = 160;
static const int HEIGHT = 160;
};
#endif
#ifndef ___TOUCHOBJECT___
#define ___TOUCHOBJECT___
#include <QtGui>
#include <QGesture>
#include <iostream>
class TouchObject : public QGraphicsObject {
Q_OBJECT
public:
TouchObject(QGraphicsItem* pParent = NULL) : QGraphicsObject(pParent) {
grabGesture(Qt::PinchGesture);
setAcceptTouchEvents(true);
}
QRectF boundingRect() const {
return QRectF(-WIDTH / 2, -HEIGHT / 2, WIDTH, HEIGHT);
}
QPainterPath shape() const {
QPainterPath path;
path.addEllipse(boundingRect());
return path;
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *pOption, QWidget *pWidget) {
Q_UNUSED(pOption)
Q_UNUSED(pWidget)
painter->setBrush(Qt::blue);
painter->drawEllipse(boundingRect());
}
bool sceneEvent(QEvent* pEvent) {
QGestureEvent* pGestureEvent ;
switch (pEvent->type()) {
case QEvent::TouchBegin:
pEvent->accept();
qDebug("Touch Event Received from sceneEvent()");
return true;
case QEvent::Gesture:
qDebug("Gesture Event Received from sceneEvent()");
pGestureEvent = static_cast<QGestureEvent*>(pEvent);
if(QGesture *pinch = pGestureEvent->gesture(Qt::PinchGesture)) {
qDebug(" Pinch Gesture: state: %d", pinch->state());
}
else {
qDebug(" not a pinch gesture.");
}
break;
default:
break;
}
return QGraphicsObject::sceneEvent(pEvent);
}
private:
static const int WIDTH = 160;
static const int HEIGHT = 160;
};
#endif
To copy to clipboard, switch view to plain text mode
main.cpp:
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include "TouchObject.h"
int main(int argc, char* argv[]){
pScene->setSceneRect(-300, -300, 600, 600);
pScene->addItem(new TouchObject());
view.viewport()->grabGesture(Qt::PinchGesture);
view.show();
return app.exec();
}
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include "TouchObject.h"
int main(int argc, char* argv[]){
QApplication app(argc, argv);
QGraphicsScene* pScene = new QGraphicsScene();
pScene->setSceneRect(-300, -300, 600, 600);
pScene->addItem(new TouchObject());
QGraphicsView view(pScene);
view.viewport()->grabGesture(Qt::PinchGesture);
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
GraphicsObjectGesture.pro:
TEMPLATE = app
CONFIG += qt warn_on no_keywords
QT +=
TARGET = GraphicsObjectGestures
SOURCES = main.cpp
HEADERS = TouchObject.h
LIBS +=
CONFIG += console
# Treat warnings as errors
win32:QMAKE_CXXFLAGS += /WX
TEMPLATE = app
CONFIG += qt warn_on no_keywords
QT +=
TARGET = GraphicsObjectGestures
SOURCES = main.cpp
HEADERS = TouchObject.h
LIBS +=
CONFIG += console
# Treat warnings as errors
win32:QMAKE_CXXFLAGS += /WX
To copy to clipboard, switch view to plain text mode
The only event I really receive is the QEvent::TouchBegin (so yes, the QGraphicsObject seems to receive touch events) - but never a QEvent::Gesture event, as it seems...
I will open a Qt issue for that and post my own example code there as well...
Cheers, Oliver
Added after 24 minutes:
I created an issue for that: http://bugreports.qt.nokia.com/browse/QTBUG-16618
Bookmarks