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. [Done] QGraphicsView: How to use both scale and rotate for QPinchGesture in touch screen?
Forum Updated to NodeBB v4.3 + New Features

[Done] QGraphicsView: How to use both scale and rotate for QPinchGesture in touch screen?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.7k 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.
  • S Offline
    S Offline
    Sam
    wrote on last edited by
    #1

    Hi,

    I am working on an application that runs on a touchscreen monitor. I have subclassed QGraphicsView class and overridden the viewportEvent() where i am writing the following code.

    @bool CustomGraphicsView::viewportEvent(QEvent *event)
    {
    switch (event->type()) {
    case QEvent::TouchBegin:
    case QEvent::TouchUpdate:
    case QEvent::TouchEnd:
    {
    QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
    QListQTouchEvent::TouchPoint touchPoints = touchEvent->touchPoints();
    if (touchPoints.count() == 2) {
    // determine scale factor
    const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
    const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();

            qreal currentScaleFactor =
                    QLineF(touchPoint0.pos(), touchPoint1.pos()).length()
                    / QLineF(touchPoint0.startPos(), touchPoint1.startPos()).length();
    
            if (touchEvent->touchPointStates() & Qt::TouchPointReleased) {
    
                totalScaleFactor *= currentScaleFactor;
                currentScaleFactor = 1;
            }
            setTransform(QTransform().scale(totalScaleFactor * currentScaleFactor,
                                            totalScaleFactor * currentScaleFactor));
    
        }
        
        return true;
    }
     default:
        break;
    }
    return QGraphicsView::viewportEvent(event);
    

    }@

    Also for the graphicsView i have set the attribute i.e

    @viewport()->setAttribute(Qt::WA_AcceptTouchEvents);@

    The above code works properly and scales the graphicsItem accordingly.
    Also inorder to rotate the same i changed the code like,

    @case QEvent::TouchEnd:
    {
    QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);

        if (touchEvent->touchPoints().count() == 2) {
            const QTouchEvent::TouchPoint &touchPoint1 = touchEvent->touchPoints().first();
            const QTouchEvent::TouchPoint &touchPoint2 = touchEvent->touchPoints().last();
    
            QLineF line1(touchPoint1.lastScenePos(), touchPoint2.lastScenePos());
            QLineF line2(touchPoint1.scenePos(), touchPoint2.scenePos());
    
            rotate(line2.angleTo(line1));
        }
    
        break;
    }@
    

    I have specified Qt::PinchGesture for graphicsView
    But now the requirement is that both scale and rotate should work simultaneously. I am able to get them working individually.
    What code should i implement so that i can use both Scaling as well as Rotation of my graphicsItem?

    Thanks for your time.

    1 Reply Last reply
    0
    • U Offline
      U Offline
      utcenter
      wrote on last edited by
      #2

      You have done the rotation which is the harder part, now just compare the initial length of the line between the two touch points to the updated/end length, and scale accordingly.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #3

        Thanks for the help, I'll look into it. Is there any way that i can also calculate the perspective/transformation for three touch points ?

        1 Reply Last reply
        0
        • U Offline
          U Offline
          utcenter
          wrote on last edited by
          #4

          Perspective in 2d? No such thing! ;)

          You can easily use 3 points as well, but it is overkill and unnecessary - you still can create a triangle and follow its transformations and apply those to the scene but it just adds to the complexity without adding any actual value. You can do it for the sake of doing it thou, you can use the area or perimeter/circumference of the triangle for scale and its orientation for rotation, but the latter you will still have to reduce to a line, i.e. follow the rotation of the triangle's height.

          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