Location>code7788 >text

Using QLineF to Extract Angles from QTransforms

Popularity:384 ℃/2024-07-25 22:23:04

When we transform a QGraphicsItem, QT provides many convenient methods. However, when we want to get the angle of the current transformation, it's a bit difficult because QTransform doesn't provide a way to get the angle. In the articleQt reverse solve Translate/Scale/Rotate analysis from QTransformAfter analyzing QTransform, it is difficult to derive the angle of the current transform from the QTransform matrix because the order of the combinations and the number of combinations cannot be predicted. Even if it can be deduced, the angle is not accurate due to the effect of tangent and scaling transformations. Combining with QLineF, there is an easy way to derive the transform angle backwards.

I. Starting with QGraphicsItem transformations

There are three ways to transform a QGraphicsItem:

1. Use setRotation() or setScale();
2. use setTransform();
3. Use setTransformations();

If more than one method is used at the same time, the transformation effects will be superimposed. Transformations are performed in a fixed order:

  • Step 1: Apply the transform specified by transform();
  • Step 2: Apply the transformations specified by transformations();
  • Step 3: Apply the transformation specified by rotation(), transformOriginPoint();
  • Step 4: Apply the transformations specified by scale(), transformOriginPoint();

If transform() specifies rotation A and rotation() specifies rotation B, then the QGraphicsItem rotates by A + B. Then resetTransform() resets transform(), and the QGraphicsItem rotates by B. This means that the three transformations do not affect each other. In other words, the three transformations do not affect each other's values.

For QGraphicsItem with parent-child relationship, the transformations of the parent item will be accumulated to the child item, which gets all the accumulated transformations via method sceneTransform() and the coordinates of the child item on the scene via scenePos().

Analyze the current rotation of the QGraphicsItem.

  1. Angles that act directly on the QGraphicsItem

To calculate the angle of the current rotation of a QGraphicsItem without taking into account the effect of the parent section item on the QGraphicsItem, or if the QGraphicsItem has no parent, you need to take into account the angles set by setRotation(), setTransform(), and setTransformations() separately, and then add them up. and setTransformations(), and then add them up. If QGraphicsItem doesn't have a parent, you can use sceneTransform() to get all the transforms and derive the angle from them.

  1. Angle of QGraphicsItem with parent project

Considering the effect of the parent item's transforms on the QGraphicsItem, use sceneTransform() directly to get the transforms of all the cumulative transforms, and then extrapolate the angle.

Using QLineF to Extract Angles from the Transform

QLineF provides a method angleTo() to help us easily calculate the angle between two lines. To calculate the angle from a transform, you build a line and then use QTransform's map() method to transform the line. The angle between the transformed line and the original line is the angle that the transform rotates. Here's the sample code:

QTransform trans = item->sceneTransform();
QLineF line1{{0,0},{1,0}};
QLineF line2 = (line1);
qreal angle = (line1)

Note: If a shear transform has been performed in transform, the angle derived by this method will not be the original angle.