Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTouchEvent Position Problem
Forum Updated to NodeBB v4.3 + New Features

QTouchEvent Position Problem

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    metRo_
    wrote on last edited by
    #1

    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;
    

    }@

    1 Reply Last reply
    0
    • A Offline
      A Offline
      agocs
      wrote on last edited by
      #2

      What about setPos(mapToParent(mapFromScene(touchPoint1.scenePos()))) ?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved