QTouchEvent Position Problem
-
I'm getting problems to set the position of the stickitem. I'm using the touchpoint.scenePos() that gives me the position of the stickitem with reference to scene but the setPos() will position the stickitem in reference to its parent, the joystick. How can I map the sceneposition to the joystick position?
Thanks :)
main.cpp
@#include <QApplication>
#include <QGraphicsView>
#include <joystick.h>
#include <stickitem.h>int main(int argc, char **argv)
{
QApplication app(argc, argv);QGraphicsScene scene; scene.setSceneRect(0,0,854,480); QGraphicsView view(&scene); view.viewport()->setAttribute(Qt::WA_AcceptTouchEvents); view.setFixedSize(854,480); view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view.setStyleSheet( "QGraphicsView { border-style: none; }" ); Joystick *left = new Joystick(200,0,350,350); scene.addItem(left); view.showFullScreen(); return app.exec();
}@
Joystick.cpp
@Joystick::Joystick(int posX, int posY, int rectWidth, int rectHeight) :
QGraphicsRectItem(0, 0, rectWidth, rectHeight)
{
setPos(posX, posY);
StickItem *stick = new StickItem(false, false, this);
}@@StickItem::StickItem(bool fixX, bool fixY, QGraphicsItem *parent, QObject *parentObject) :
QObject(parentObject), QGraphicsEllipseItem(0,0,100,100,parent)
{
setPos(0, 0);
setAcceptTouchEvents(true);parentRect = new QRectF(parent->boundingRect()); qDebug()<<parent->boundingRect();
}
bool StickItem::sceneEvent(QEvent *event)
{
switch (event->type()) {
case QEvent::TouchBegin:
{
event->accept();
break;
}
case QEvent::TouchUpdate:
{
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
QTouchEvent::TouchPoint touchPoint1 = touchEvent->touchPoints().first();
//qDebug()<<"POS:"<<touchPoint1.pos().x()<<touchPoint1.pos().y();
qDebug()<<"SCENEPOS:"<<touchPoint1.scenePos().x()<<touchPoint1.scenePos().y();
qDebug()<<"MAPSCENE:"<<mapFromParent(touchPoint1.scenePos());
qDebug()<<"-----------------------------------------------------";
setPos(mapToScene(0,0));
//setPos(touchPoint1.scenePos());
touchEvent->accept();
break;
}
case QEvent::TouchEnd:
{
break;
}
default:
return QGraphicsItem::sceneEvent(event);
}return true;
}@
-
What about setPos(mapToParent(mapFromScene(touchPoint1.scenePos()))) ?