我已经用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;
}
我的问题是,当我触摸物品时,右上角的项目会到达触摸点。 我想抵消我触摸的内部点。 但是,当我这样做时,我一次只能移动一个项目。
好的,回答我自己的问题:
setPos ( touchPoint1.scenePos() - touchPoint1.pos());
不正确。 在 TouchBegin 上,我应该存储 touchPoint1.pos():
m_TouchOffset = touchPoint1.pos();
然后改用第一个位置
setPos ( touchPoint1.scenePos() - m_TouchOffset);