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