PDA

View Full Version : Issues with mapping with QTransform



spawn9997
4th January 2011, 00:01
I have a set of points defines in world coordinates. I draw these points on to a pixmap and I have made use of a QTransform to map points from say... a scene rectangle of QRectF(1000,2000,4000,3000) to a pixmap of size (1000,1000). O.k. now I want to scale the points such that if the scene changes to QRectF(1000,2000,4000,1500) Instead of my points getting squeezed by half the height, I want the points to be scaled but also translated such that the 2 rectangles end up with the same center. In this way, if the height is half then the center point will be the same but the topLeft() and bottomRight() points will be adjusted.

I have definitely looked at the code on QTransform and mapping. Matrices are not my strong point. Maybe I am missing a simple step.

any help would be appreciated,

spawn9997

pkohut
4th January 2011, 00:13
Is the pixmap a scene item too?

spawn9997
4th January 2011, 16:21
Is the pixmap a scene item too?

The way it is setup, the QGraphicsItem displays the 'correct' pixmap depending on level of detail. Part of the function of 'MyGraphicsItem' is to initially draw itself onto a pixmap and then use the painter function QPainter::drawPixmap() in the QGraphicsItem::paint() function.

Btw, using Qt 4.6.2.

pkohut
4th January 2011, 16:45
Can you calc the mid point of the source and then setPos on the pixmapItem?

Totally not tested (or compiled), but looks correct


QVector2d vTopLeft(rect.topLeft());
QVector2d vMid = vTopLeft + ((QVector(rect.bottomRight()) - vTopLeft) * 0.5);
pixmap->setPos(vMid->toPointF());

spawn9997
5th January 2011, 23:40
@pkohut I actually figured it out before I looked at this. I basically did exactly what you suggested. I forgot to set the new scaled Rectangle's center to the old Rectangle's center. The scaled rectangle is used to map points to the pixmap. I was mapping to a rectangle at the wrong position, Doh! When I called

QRectF tmp(x,y,w,h);
tmp.moveCenter(m_realRect.center());
and then mapped the points to this rectangle, everything looked exactly as it was supposed to.

pkohut
5th January 2011, 23:50
QRectF tmp(x,y,w,h);
tmp.moveCenter(m_realRect.center());


Ha, scanned right past center and moveCenter, was looking for middle. :)