Understanding Qt's QTransform rotate function

2019-06-23 17:04发布

As a result of this question, I'd like to understand more about Qt's QTransform::rotate function. In the documentation, it says:

QTransform & QTransform::rotate ( qreal angle, Qt::Axis axis = Qt::ZAxis )

Rotates the coordinate system counterclockwise by the given angle about the specified axis and returns a reference to the matrix.

Note that if you apply a QTransform to a point defined in widget coordinates, the direction of the rotation will be clockwise because the y-axis points downwards.

The angle is specified in degrees.

From my previous question, I learned that to rotate a QPolygonF clockwise, I must actually rotate it 90 degrees counter-clockwise, according to the rotate function:

QPolygonF original = QPolygonF() << QPoint(0, 1) << QPoint(4, 1) << QPoint(4, 2) << QPoint(0, 2);
QTransform transform = QTransform().translate(2, 2).rotate(90).translate(-2, -2);
QPolygonF rotated = transform.map(original);
qDebug() << rotated;

Output:

QPolygonF(QPointF(3, 0) QPointF(3, 4) QPointF(2, 4) QPointF(2, 0) ) 

E.g. for this rectangle:

original rectangle rotation

To rotate to here:

new rectangle rotation

Why is this? Why does the documentation say that my call to QTransform::rotate is actually causing a clockwise rotation when I believe I'm not in "widget coordinates" - there are no widgets involved here.

2条回答
一夜七次
2楼-- · 2019-06-23 17:47

Actually I think the problem is that the function in question rotates the coordinate system - not the object. If you want to rotate your object clockwise, that's equivalent to rotating the coordinate system counter-clockwise.

查看更多
何必那么认真
3楼-- · 2019-06-23 17:53

QTransform operates in mathematical Cartesian coordinate system. ie. the top-right quadrant is the positive-x and positive-y one (quadrant 1). Your chart is in the y-axis-flipped Widget coordinates with quadrant 1 at lower-right.

Rotate does rotate counterclockwise. But since Y-axis is flipped in Widget coordinates, it becomes clockwise.

As you mentioned, polygon has nothing to do with widgets. It does rotate CCW, but you are visualizing it with widget coordinates.

查看更多
登录 后发表回答