Important: Please read the Qt Code of Conduct -

How to get the angle from a QTransform variable?

  • Hi,

    I am working with QGraphicsItem objects and I need to get the angle from one item to set it to another one. In the context of my requirement, I can't get it directly from the QGraphicsItem variable (using the rotation() method) but from a QTransform object that I stored previously.

    Now, is it possible to extract the angle value from a QTransform variable?

    Thanks! :)

  • I don't think you can disentangle the rotation element from the other operations you performed. So, unless all you did to your QTransform is apply a rotation then it's impossible to calculate (some math genius might point out I'm wrong here btw)

    The math-illiterate answer is,0.0,1.0,0.0)).angle();

  • Qt Champions 2017

    Depends on what you put in your transform. Did you do any scaling, reflection, translation? If you did not, then:

    qreal angle = std::acos(transform.m11());

    will give you your angle (in radians).

  • @kshegunov
    You are correct, but even if there were other operations, all is not lost. Recall that the inverse of matrix A multiplied by the matrix A yields, I, the identity matrix. So if you have RS, a rotation matrix multiplied by a scale matrix, then RSS(inverse) yields R, from which you can derive the angle. (Note, however, that S(inverse)RS will not yield R.) You can repeat the inverse process until you obtain R.

  • @ofmrew That assumes you know S in isolation which is probably not the case if you just have a QTransform object.

    For the men-in-the-street the post above means that if you know what operations you did (scale in the example) you can always revert them (scale back in the example) until you are left with just the rotation.

Log in to reply