[Qt-interest] cannot grab gesture with qgraphicsobject

Sean Hayes sean.t.hayes at vanderbilt.edu
Thu Mar 10 17:17:42 CET 2011


The problem is that in your TabGestureRecognizer you are setting the result
to canceled for gestures that have not been triggered (e.g., non-touch
events). Note the changes I made to your recognize() function below. Also,
note that you do not need the mouse event code there.

QGestureRecognizer::Result TapGestureRecognizer::recognize(QGesture *state,
QObject *, QEvent *event)
{
TapGesture *q = static_cast<TapGesture *>(state);
const QTouchEvent *ev = static_cast<const QTouchEvent *>(event);
QGestureRecognizer::Result result = *QGestureRecognizer::Ignore*;

switch (event->type()) {
case QEvent::TouchBegin: {
q->setPosition(ev->touchPoints().at(0).pos());
q->setHotSpot(ev->touchPoints().at(0).screenPos());
q->start();
result = QGestureRecognizer::MayBeGesture;
break;
}
case QEvent::TouchUpdate:
case QEvent::TouchEnd: {
if (ev->touchPoints().size() == 1) {
QTouchEvent::TouchPoint p = ev->touchPoints().at(0);
QPoint delta = p.pos().toPoint() - p.startPos().toPoint();
if (delta.manhattanLength() <= Delta && q->elapsed() <= TimerInterval &&
event->type() == QEvent::TouchEnd)
result = QGestureRecognizer::TriggerGesture;
else* if(q->state() == Qt::GestureUpdated || q->state() ==
Qt::GestureStarted)*
result = QGestureRecognizer::CancelGesture;
}
break;
}
}
return result;
}

On Wed, Mar 9, 2011 at 10:34 AM, Wathek LOUED <wathek at gmail.com> wrote:

> Hi Till,
>
> Thank you for your response I've already seen your bug post but
> unfortunatly that doesn't help. Here are some part of my code :
>
> *gesturerecognizer class :*
> *
> *
> #ifndef GESTURERECOGNIZER_H
> #define GESTURERECOGNIZER_H
>
> #include "tapgesture.h"
> #include "tapandholdgesture.h"
>
> #include <QGestureRecognizer>
> #include <QGesture>
> #include <QEvent>
> #include <QObject>
> #include <QHash>
>
> class TapGestureRecognizer: public QGestureRecognizer
> {
> public:
> TapGestureRecognizer();
>  QGesture *create(QObject *target);
> QGestureRecognizer::Result recognize(QGesture *state, QObject *watched,
> QEvent *event);
>  void reset(QGesture *state);
> private:
> enum { TimerInterval = 400  };
> enum { Delta = 20 };
>  QHash<long, TapGesture *> touchPoints;
> };
> #endif
>
> #include "gesturerecognizer.h"
>
> #include <QWidget>
> #include <QTouchEvent>
> #include <QDebug>
> /*******************************************************
>  * TapGesture Recognizer
>  *******************************************************/
> TapGestureRecognizer::TapGestureRecognizer()
> {}
>
> QGesture *TapGestureRecognizer::create(QObject *target)
> {
> if (target && target->isWidgetType())
>  static_cast<QWidget *>(target)->setAttribute(Qt::WA_AcceptTouchEvents);
> return new TapGesture;
> }
>
> QGestureRecognizer::Result TapGestureRecognizer::recognize(QGesture *state,
> QObject *, QEvent *event)
> {
> TapGesture *q = static_cast<TapGesture *>(state);
>  const QTouchEvent *ev = static_cast<const QTouchEvent *>(event);
> QGestureRecognizer::Result result = QGestureRecognizer::CancelGesture;
>
> switch (event->type()) {
> case QEvent::TouchBegin: {
> q->setPosition(ev->touchPoints().at(0).pos());
>  q->setHotSpot(ev->touchPoints().at(0).screenPos());
> q->start();
> result = QGestureRecognizer::MayBeGesture;
>  break;
> }
> case QEvent::TouchUpdate:
> case QEvent::TouchEnd: {
>  if (ev->touchPoints().size() == 1) {
> QTouchEvent::TouchPoint p = ev->touchPoints().at(0);
> QPoint delta = p.pos().toPoint() - p.startPos().toPoint();
>  if (delta.manhattanLength() <= Delta && q->elapsed() <= TimerInterval &&
> event->type() == QEvent::TouchEnd)
> result = QGestureRecognizer::TriggerGesture;
>  else
> result = QGestureRecognizer::CancelGesture;
> }
>  break;
> }
> case QEvent::MouseButtonPress:
> case QEvent::MouseMove:
>  case QEvent::MouseButtonRelease: {
> result = QGestureRecognizer::Ignore;
> break;
>  }
> default: {
> result = QGestureRecognizer::Ignore;
>  break;
> }
> }
>
> return result;
> }
>
> void TapGestureRecognizer::reset(QGesture *state)
> {
> TapGesture *q = static_cast<TapGesture *>(state);
> q->setPosition(QPointF());
>  QGestureRecognizer::reset(state);
> }
>
>
> *My QGraphicsView class :*
> *
> *
> #ifndef FRMACCUEIL_H
> #define FRMACCUEIL_H
>
> #include "gesture/gesturerecognizer.h"
>
> #include <QGraphicsView>
> #include <QGraphicsScene>
> #include <QGestureEvent>
> #include <QEvent>
> #include <QTouchEvent>
> #include <QHash>
>
> class FrmAccueil: public QGraphicsView
> {
> Q_OBJECT
> public:
> FrmAccueil(QGraphicsScene *scene=0, QWidget *parent=0);
> bool viewportEvent(QEvent *event);
>
> private:
> void gestureEvent(QGestureEvent *event);
> Qt::GestureType tapGesture;
>  TapGestureRecognizer *tapGestureRecognizer;
> };
>
> #endif
>
> #include "frmaccueil.h"
>
> #include <QDebug>
> #include <QGestureRecognizer>
>
> FrmAccueil::FrmAccueil(QGraphicsScene *scene, QWidget *parent)
>    :QGraphicsView(scene, parent)
> {
> tapGestureRecognizer = new TapGestureRecognizer();
>
> tapGesture = QGestureRecognizer::registerRecognizer(tapGestureRecognizer);
>
>         viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
> viewport()->grabGesture(tapGesture);
> }
>
> bool FrmAccueil::viewportEvent(QEvent *event)
> {
> if (event->type() == QEvent::Gesture)
> gestureEvent(static_cast<QGestureEvent*>(event));
>
> return QGraphicsView::viewportEvent(event);
> }
>
> void FrmAccueil::gestureEvent(QGestureEvent *event)
> {
>  if (event->gesture(tapGesture))
> qDebug("TAP GESTURE");
> }
>
> *My QGraphicsScene class:*
>
> #ifndef PUSHBUTTONITEM_H
> #define PUSHBUTTONITEM_H
>
> #include "gesture/gesturerecognizer.h"
>
> #include <QGraphicsObject>
> #include <QEvent>
> #include <QGestureEvent>
> #include <QPixmap>
> #include <QPainter>
>
> class PushButtonItem:public QGraphicsObject
> {
> Q_OBJECT
> public:
> enum ShapeMode {
> MaskShape,
> BoundingRectShape,
>  HeuristicMaskShape
> };
>
> PushButtonItem(QGraphicsItem *parent=0);
>  QPainterPath shape() const;
>
> /* QGraphicsPixmapItem */
> QPixmap pixmap() const;
>  void setPixmap(const QPixmap &pixmap);
>
> Qt::TransformationMode transformationMode() const;
>  void setTransformationMode(Qt::TransformationMode transformation);
>
> QRectF boundingRect() const;
>  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
> QWidget *widget=0);
>
> protected:
> bool sceneEvent(QEvent *event);
>
> private:
> void gestureEvent(QGestureEvent *event);
>
> ButtonType type;
>
> QList<GraphicsLineItem *> edgeList;
>  TapGestureRecognizer *tapGestureRecognizer;
> Qt::GestureType tapGesture;
>
> // QGraphicsPixmapItem
> QPixmap pix;
> bool hasShape;
>  Qt::TransformationMode transfMode;
> };
>
> #endif
>
> #include "pushbuttonitem.h"
>
> #include <QPixmap>
>
> #include <QDebug>
> #include <QTouchEvent>
> #include <QGestureRecognizer>
>
> PushButtonItem::PushButtonItem(QGraphicsItem *parent)
>        :QGraphicsObject(parent)
> {
> setFlags(QGraphicsItem::ItemIsMovable |
> QGraphicsItem::ItemSendsGeometryChanges);
>
> setTransformationMode(Qt::SmoothTransformation);
>  setAcceptTouchEvents(true);
>
> tapGestureRecognizer = new TapGestureRecognizer();
>  tapGesture =
> QGestureRecognizer::registerRecognizer(tapGestureRecognizer);
>
> grabGesture(tapGesture);
> }
>
> QPainterPath PushButtonItem::shape() const
> { return QGraphicsItem::shape();     }
>
> bool PushButtonItem::sceneEvent(QEvent *event)
> {
> qDebug("Event received");
> if (event->type() == QEvent::Gesture)
>  gestureEvent(static_cast<QGestureEvent *>(event));
> return true;
>
> }
>
> void PushButtonItem::gestureEvent(QGestureEvent *event)
> {
> if (event->gesture(tapGesture))
> qDebug("button Tapped");
> }
>
> /***************************************************************
>  * QGraphicsPixmapItem Reimplementation
>  ***************************************************************/
>
>
> I cannot really see where's the problem the QGraphicsView receive the
> gestureEvent but not the QGraphicsObject.
>
>
> Thank you so much
>
> Sincerely
> W.L
>
> On Mon, Mar 7, 2011 at 11:02 PM, Till Oliver Knoll <
> till.oliver.knoll at gmail.com> wrote:
>
>> Am 07.03.11 19:31, schrieb Wathek LOUED:
>>  > Hi,
>>  >
>>  > I got a problem with QGraphicsObject, I cannot grab gesture and I'm
>>  > getting this message : QGestureManager::deliverEvent: could not find
>> the
>>  > target for gesture.
>>
>> My experience up to Qt 4.7.1 is that QGestures are not as "mature" as
>> one would wish, especially on Mac.
>>
>> I had a somewhat similar issue that Pan gesture would not get through to
>> QGraphicsObjects in a QGraphicsScene. It turned out that this is a
>> "design issue" (how Gestures are implemented respective how items in a
>> scene deal with events) and won't be fixed:
>>
>>   http://bugreports.qt.nokia.com/browse/QTBUG-16618
>>
>> There is an example attached which shows how gestures SHOULD work with
>> QGraphicsObjects. Note that up to Qt 4.6.x also the QGraphicsView (the
>> view widget thereof!) had to grab the same gestures as the item itself.
>> This should not be necessary anymore with Qt 4.7.x.
>>
>> So in my case the QGraphicsObject did not receive Pan gesture events
>> (only the "start" event, but never the "update" or "end" events). Never
>> tried "Tab" events, but I got "pinch" (zoom/rotate) to work.
>>
>> But just now I am investigating another issue: it seems that the value of
>>
>>  http://doc.trolltech.com/4.7/qpinchgesture.html#scaleFactor-prop
>>
>> returns different signed values on Mac and Windows 7. I developped my
>> app on Mac and implemented my "zoom" in such a way that when moving the
>> fingers apart the object would get bigger.
>>
>> A quick test on some netbook and laptop running Windows 7 revealed that
>> the "zoom" would work the other way (the object was scaled down instead
>> of scaled up). So I assume it is the sign which is different on Mac and
>> Windows 7 (= bug). But again, I did not have the opportunity yet to
>> reproduce this behaviour with debugging possibilities.
>>
>>
>> Hope that gives you some pointers,
>>   Oliver
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at qt.nokia.com
>> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110310/0d3146d9/attachment.html 


More information about the Qt-interest-old mailing list