Question

I have implemented QGraphicsObject with QTouchEvents and reimplemented the sceneEvent function.

bool LynxItem::sceneEvent(QEvent *event)
{
    //qDebug() << "LynxItem::sceneEvent:" << itemId;
    switch (event->type()) {
    case QEvent::Gesture:
    {
         qDebug() << "LynxItem::sceneEvent:Gesture" << itemId;
         return gestureEvent(static_cast<QGestureEvent*>(event));
         break;
    }
    case QEvent::TouchBegin:
    case QEvent::TouchUpdate:
    case QEvent::TouchEnd:
    {
        qDebug() << "LynxItem::sceneEvent:TouchUpdate" << itemId;
        QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
        QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
        const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
        /* This doesn't allow two objects to move at the same time */
        //setPos ( touchPoint1.scenePos() - touchPoint1.pos());
        /* This does work but the item is always moved from top left */
        setPos(touchPoint1.scenePos());
        event->accept();
        break;
    }
    default:
        return QGraphicsItem::sceneEvent(event);;
    }
    return true;
}

My problem is that when I touch the item the items top right corner comes to the touch point. I want to offset the point inside where I touched. However when I do that I can only move one item at a time.

Was it helpful?

Solution

Ok, to answer my own question:

setPos ( touchPoint1.scenePos() - touchPoint1.pos());

Is incorrect. On the TouchBegin I should store touchPoint1.pos():

m_TouchOffset = touchPoint1.pos();

Then use that first position instead

setPos ( touchPoint1.scenePos() - m_TouchOffset);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top