[Done] QGraphicsView: How to use both scale and rotate for QPinchGesture in touch screen?
-
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.
-
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.