PDA

View Full Version : QTransform rotation and scale issue



Talei
25th March 2014, 01:12
Hello,

What I want to do is to scale and rotate + translate object but I do have problem with scale + rotate.

I have item (QGraphicsPathItem, I do setPath to QRect...), item is square i.e. 100x100, and I do scale item with:

- translate to topLeft boundingBox point
- scale
- translate (back) to -topLeft boundingBox point


QTransform scale= transform();
scale.translate( boundingRect().topLeft().x(), boundingRect().topLeft().y());
scale..scale(2, 4);
scale.translate( -boundingRect().topLeft().x(), -boundingRect().topLeft().y());
so now square is rectangle.

Now I want to rotate that rectangle, so I do:

- translate to center boundingBox point
- rotate
- translate (back) to -center boundingBox point


QTransform rot= transform();
rot.translate( boundingRect().center().x(), boundingRect().center().y());
rot.rotate( angle);
rot.translate( -boundingRect().center().x(), -boundingRect().center().y());

What I would expect to happen is that rectangle will rotate by angle around boundingBox center point.
But what I do got is that path that is painted is painted inside on boundingBox and that bounding box is not rotated.

When I do debug transformation type on paint() event I do see TxShare and I think that I should see TxRotate (top of type enum, so translate + scale + rotate).

I did attached screens of what I go get when I do rotate scaled item and what I want to get.
From left: oryginal -> scaled -> +rotated -> +rotated -> +rotated. Rotation in the same direction.
10165

So to me it seams like order of transform is changed, it should be translate -> scale -> rotate and from screen it seams like it's translate -> rotate -> scale. Or maybe it's translate -> scale -> rotate -> shear but i didn't set shear.

Thanks for any help.

Talei
25th March 2014, 16:53
Rotating around different point seams to not change the issue. It's like scale is done on horizontal and vertical axis.

The code that I use to scale and rotate object (keyPressEvent - rotate on A and scale on S)


void GraphicsPathItem::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_A) {

qDebug() << "Rotating item";

QTransform itTransf = transform();
QPointF dp = this->boundingRect().center();

itTransf.translate( dp.x(), dp.y());
itTransf.rotate( rotation() + 5, Qt::ZAxis);
itTransf *= QTransform::fromScale( scale(), scale());
itTransf.translate( -dp.x(), -dp.y());

setTransform(itTransf);

qDebug() << "Current transform:" << this->transform();
}

if (e->key() == Qt::Key_S) {

qDebug() << "Scale item";

QTransform itTransf = transform();
QPointF dp = this->boundingRect().center();

itTransf.translate( dp.x(), dp.y());
itTransf *= QTransform::fromScale( scale() + 1.1, scale() + 2.5);
itTransf.translate( -dp.x(), -dp.y());

setTransform(itTransf);
qDebug() << "Current transform:" << this->transform();
}

if (e->key() == Qt::Key_R) {

qDebug() << "Reset transform";

QTransform itTransf = transform();

itTransf.reset();

setTransform(itTransf);
qDebug() << "Current transform:" << this->transform();
}

return QGraphicsPathItem::keyPressEvent( e);
}

Result:

10171

I prepared compilable example to illustrate problem. 10170