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 Error touch position

QTouchEvent Error touch position

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.2k Views
  • 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

    Hi,
    After read and try this example http://doc.qt.nokia.com/qt-maemo/touch-knobs.html I'm trying to do a similar thing where i move a ellipse.

    My code is:

    @#ifndef JOYSTICK_H
    #define JOYSTICK_H

    #include <QGraphicsItem>

    class joystick : public QGraphicsEllipseItem
    {
    public:
    joystick();

    bool sceneEvent(QEvent *event);
    

    };

    #endif // JOYSTICK_H
    @

    @#include "joystick.h"

    #include <QBrush>
    #include <QTouchEvent>

    #include <QDebug>

    joystick::joystick()
    : QGraphicsEllipseItem(0, 0, 90, 90)
    {
    setAcceptTouchEvents(true);
    setBrush(Qt::lightGray);
    }

    bool joystick::sceneEvent(QEvent *event)
    {
    switch (event->type()) {
    case QEvent::TouchBegin:
    event->accept();
    break;
    case QEvent::TouchUpdate:
    case QEvent::TouchEnd:
    {
    QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
    if (touchEvent->touchPoints().count() == 1) {
    QTouchEvent::TouchPoint touchPoint1 = touchEvent->touchPoints().first();
    qDebug()<<touchPoint1.pos();
    setPos(touchPoint1.pos());
    }

        touchEvent->accept();
        break;
    }
    default:
        return QGraphicsItem::sceneEvent(event);
    }
    
    return true;
    

    }@

    and main.cpp

    @#include <QApplication>
    #include <QGraphicsView>

    #include "joystick.h"

    int main(int argc, char **argv)
    {
    QApplication app(argc, argv);

    QGraphicsScene scene;
    QGraphicsView view(&scene);
    view.viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
    
    joystick *joy1 = new joystick;
    
    joy1->setPos(-150,0);
    
    scene.addItem(joy1);
    
    view.showFullScreen();
    view.fitInView(scene.sceneRect(), Qt::KeepAspectRatio);
    
    return app.exec();
    

    }@

    while the knobs works great my code give me bad touch position. This is the log of a line moving the touchpoint:

    QPointF(55.1084, 32.283)
    QPointF(133.696, -52.8265)
    QPointF(54.1301, 32.283)
    QPointF(132.718, -52.8265)
    QPointF(53.1519, 32.283)
    QPointF(131.739, -52.8265)
    QPointF(51.1954, 32.283)
    QPointF(130.761, -52.8265)
    QPointF(49.2388, 32.283)
    QPointF(129.783, -52.8265)
    QPointF(48.2606, 32.283)
    QPointF(128.805, -52.8265)
    QPointF(46.3041, 35.2178)
    QPointF(122.935, -51.8482)
    QPointF(45.3258, 35.2178)
    QPointF(120.979, -50.87)
    QPointF(43.3693, 35.2178)
    QPointF(119.022, -50.87)
    QPointF(41.4128, 35.2178)
    QPointF(117.065, -50.87)
    QPointF(39.4562, 35.2178)
    QPointF(115.109, -49.8917)
    QPointF(37.4997, 35.2178)
    QPointF(113.152, -49.8917)
    QPointF(35.5432, 35.2178)
    QPointF(112.174, -49.8917)
    QPointF(33.5867, 35.2178)
    QPointF(110.218, -49.8917)
    QPointF(32.6084, 35.2178)
    QPointF(108.261, -48.9134)
    QPointF(31.6302, 35.2178)
    QPointF(107.283, -48.9134)
    QPointF(30.6519, 35.2178)
    QPointF(104.348, -48.9134)
    QPointF(28.6954, 35.2178)
    QPointF(101.413, -48.9134)
    QPointF(26.7389, 36.1961)
    QPointF(99.4568, -48.9134)
    QPointF(24.7823, 36.196)
    QPointF(98.4785, -48.9134)
    QPointF(22.8258, 36.196)
    QPointF(96.522, -48.9134)
    QPointF(21.8475, 36.196)
    QPointF(94.5655, -48.9134)
    QPointF(19.891, 36.196)
    QPointF(91.6307, -47.9352)
    QPointF(17.9345, 36.196)
    QPointF(90.6525, -47.9352)
    QPointF(16.9562, 36.196)
    QPointF(89.6742, -47.9352)
    QPointF(15.978, 36.196)
    QPointF(88.696, -47.9352)
    QPointF(14.9997, 37.1743)
    QPointF(86.7394, -47.9352)
    QPointF(14.0214, 37.1743)
    QPointF(84.7829, -47.9352)
    QPointF(13.0432, 37.1743)
    QPointF(82.8264, -47.9352)
    QPointF(11.0866, 37.1743)
    QPointF(81.8481, -47.9352)
    QPointF(10.1084, 38.1526)
    QPointF(80.8699, -47.9352)
    QPointF(8.15185, 38.1526)
    QPointF(78.9134, -47.9352)
    QPointF(7.17359, 38.1526)
    QPointF(76.9568, -47.9352)
    QPointF(5.21707, 38.1526)
    QPointF(75.0003, -47.9352)
    QPointF(4.23881, 38.1526)
    QPointF(73.0438, -46.9569)
    QPointF(3.26054, 38.1526)
    QPointF(72.0655, -46.9569)
    QPointF(2.28229, 38.1526)
    QPointF(71.0873, -46.9569)
    QPointF(-1.63075, 38.1526)
    QPointF(68.1525, -46.957)
    QPointF(-3.58727, 38.1526)
    QPointF(66.196, -45.9787)
    QPointF(-4.56554, 38.1526)
    QPointF(64.2394, -45.9787)
    QPointF(-6.52206, 38.1526)
    QPointF(63.2612, -45.9787)
    QPointF(-7.50032, 38.1526)
    QPointF(62.2829, -45.9787)
    QPointF(-8.47858, 38.1526)
    QPointF(59.3481, -45.0004)
    QPointF(-10.4351, 38.1526)
    QPointF(58.3699, -45.0004)
    QPointF(-12.3916, 38.1526)
    QPointF(57.3916, -45.0004)
    QPointF(-16.3047, 38.1526)
    QPointF(56.4134, -44.0222)
    QPointF(-18.2612, 38.1526)
    QPointF(55.4351, -44.0222)
    QPointF(-19.2395, 38.1526)
    QPointF(54.4569, -44.0222)
    QPointF(-20.2177, 38.1526)
    QPointF(52.5003, -44.0222)
    QPointF(-22.1742, 38.1526)
    QPointF(50.5438, -43.0439)
    QPointF(-26.0873, 38.1526)
    QPointF(49.5656, -43.0439)
    QPointF(-28.0438, 38.1526)
    QPointF(47.609, -43.0439)
    QPointF(-30.0003, 38.1526)
    QPointF(46.6308, -43.0439)
    QPointF(-30.9786, 38.1526)
    QPointF(45.6525, -42.0657)
    QPointF(-31.9569, 38.1526)
    QPointF(44.6743, -42.0657)
    QPointF(-33.9134, 38.1527)
    QPointF(42.7177, -42.0657)
    QPointF(-36.8482, 38.1527)
    QPointF(40.7612, -42.0657)
    QPointF(-39.783, 38.1527)
    QPointF(38.8047, -42.0657)
    QPointF(-41.7395, 39.1309)
    QPointF(35.8699, -42.0657)
    QPointF(-44.6743, 39.1309)
    QPointF(33.9134, -42.0657)
    QPointF(-46.6308, 39.1309)
    QPointF(31.9569, -42.0657)
    QPointF(-47.6091, 39.1309)
    QPointF(30.9786, -42.0657)
    QPointF(-48.5873, 39.1309)
    QPointF(29.0221, -42.0657)
    QPointF(-49.5656, 39.1309)
    QPointF(28.0439, -42.0657)
    QPointF(-50.5439, 40.1092)
    QPointF(26.0873, -42.0657)
    QPointF(-51.5221, 40.1092)
    QPointF(24.1308, -42.0657)
    QPointF(-52.5004, 40.1092)
    QPointF(23.1526, -42.0657)
    QPointF(-53.4786, 40.1092)
    QPointF(22.1743, -42.0657)
    QPointF(-53.4786, 40.1092)
    QPointF(22.1743, -42.0657)

    As you can see the value change a lot but with knobs example it simple work. Another thing is if i just click it moves for another position :s

    I recorded a simple movie to ilustrate the problem: http://www.youtube.com/watch?v=tDK2Ta2IoCY

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      The coordinates returned by pos() are relative to the object that received the touch event. You may want to use scenePos() to return absolute coordinates.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        metRo_
        wrote on last edited by
        #3

        Thanks :)

        [quote author="mlong" date="1331258429"]The coordinates returned by pos() are relative to the object that received the touch event. You may want to use scenePos() to return absolute coordinates.[/quote]

        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