0

我已经用 QTouchEvents 实现了 QGraphicsObject 并重新实现了 sceneEvent 函数。

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;
}

我的问题是,当我触摸项目时,项目右上角会到达接触点。我想抵消我触摸的点。但是,当我这样做时,我一次只能移动一个项目。

4

1 回答 1

1

好的,回答我自己的问题:

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

是不正确的。在 TouchBegin 上,我应该存储 touchPoint1.pos():

m_TouchOffset = touchPoint1.pos();

然后改用第一个位置

setPos ( touchPoint1.scenePos() - m_TouchOffset);
于 2011-05-12T12:30:11.230 回答