开发者

Why am I unable to perform a calculation for the position in a sceneEvent for a QGraphicsObject?

开发者 https://www.devze.com 2023-03-05 21:09 出处:网络
I have implemented QGraphicsObject with QTouchEvents and reimplemented the sceneEvent function. bool LynxItem::sceneEvent(QEvent *event)

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 a开发者_如何学Ct a time.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号