Results 1 to 10 of 10

Thread: Set the rotation of a QTransform

  1. #1
    Join Date
    Jul 2015
    Posts
    52
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Set the rotation of a QTransform

    I have a QGraphicsView that is transformed (scaled, translated, rotated) by a few functions.

    At a given point, I'd like to set its rotation to a certain value. Even better, I'd like to have its scale, translation and rotation. However, the goal is to change its rotation but keep the translation and scale.

    I don't need to have the original transformation order.

    I've only seen .rotate() but there's no .setRotation() and I am not sure how to retrieve the rotation angle correctly. At the moment there's no shear, so I've read that "The simplest general way is to transform (0,0) and (1,0), then use trigonometric functions (arctan) to get the angle".

    But I'd would like to know how this is usually done. At the moment I keep track of the scale and rebuild a new transform, but it's far from ideal and it would break if other transformations were made.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set the rotation of a QTransform

    If you always rotate around the same origin, then you could simply accumulate the angle values in another variable.

    Cheers,
    _

  3. #3
    Join Date
    Jul 2015
    Posts
    52
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set the rotation of a QTransform

    That's what I ended up doing, but it's not very satisfactory as method...

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set the rotation of a QTransform

    Why not? Keeping information that you have is usually way better and trying to recover information after throwing it away.

    Cheers,
    _

  5. #5
    Join Date
    Jul 2015
    Posts
    52
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set the rotation of a QTransform

    It seems to me a QTransform should be able to know its rotation, as well as being able to set it to a fixed value. Then the object itself knows its status rather than relaying on an object upper in the hierarchy, like QMainWindow or having to create a wrapping widget to store that value. It feels to me that storing the value separately is error prone, specially because you can't set the rotation, but you have to rotate relative to that one value you stored. But, sure, if you're careful and deal with the visibility of it, it's ok as a

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set the rotation of a QTransform

    A QTransform is a transformation value matrix.
    It does not have anything like a rotation value.

    Any transformation step that is applied is a matrix multiplication of the existing matrix values and a matrix with values depending on the type of transformation.

    The QTransform object doesn't need the sequence of transformation steps, applying each step on each point would be very wasteful.
    Instead it contains the final values that need to be applied to each point.

    Cheers,
    _

  7. #7
    Join Date
    Jul 2015
    Posts
    52
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set the rotation of a QTransform

    And that's exactly what I was asking for, the final rotation value.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set the rotation of a QTransform

    Quote Originally Posted by quimnuss View Post
    And that's exactly what I was asking for, the final rotation value.
    Yes, you did.
    And I pointed out that you can accumulate that value by tracking which rotation values you apply.

    Cheers,
    _

  9. #9
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: Set the rotation of a QTransform

    Keeping the transformation values in your own variables and rebuilding the QTransform when you need it is the simplest way (I would go with that as well).

    But if you really need to modify the rotation from the QTransform, the first two values of the first and second rows ( [m11,m12] and [m21,m22]) describe two 2D vectors that are rotated around an origin. One vector represents the horizontal axis and the other the vertical axis of this transformed space.
    They're usually perpendicular. When there's shearing involved they're not perpendicular, this is what causes shearing after all.

    The length of these vectors is the scale.
    Translation is not involved in this, it's the third row.

    So if you want to change the rotation while preserving the scale, first obtain the length of each vector (Pythagorean theorem) , rotate the vectors by the desired amount and scale them by the original length.

    vecHx = Cos( angle ) * originalScaleH
    vecHy = Sin( angle ) * originalScaleH

    vecVx = Cos( angle - 90 ) * originalScaleV
    vecVy = Sin( angle - 90 ) * originalScaleV

    [vecHx,vecHy] = [m11,m12]
    [vecVx,vecVy] = [m21,m22]

    If I'm not mistaken it's as simple as that. The "-90" might be wrong depending on the orientation expected by Qt (in which case "+90" would be the right choice, or something else).
    Last edited by Kryzon; 8th July 2015 at 02:52.

  10. #10
    Join Date
    Jul 2015
    Posts
    52
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set the rotation of a QTransform

    Thanks Kryzon,

    Since apparently the object's QTransform doesn't provide it, I'll go for the simple way and store the rotation and scale on my own. I'd play with the transformation affine matrix components if it became cumbersome to track the rotation.

Similar Threads

  1. QTransform rotation and scale issue
    By Talei in forum Qt Programming
    Replies: 1
    Last Post: 25th March 2014, 17:53
  2. Replies: 0
    Last Post: 29th September 2011, 00:18
  3. QTransform()
    By mkind in forum Newbie
    Replies: 0
    Last Post: 19th February 2010, 22:45
  4. GraphicsItemChange and QTransform
    By ct-xuyaojun in forum Qt Programming
    Replies: 0
    Last Post: 28th September 2009, 10:16
  5. QTransform + drawLine ?
    By verburg in forum Qt Programming
    Replies: 0
    Last Post: 23rd January 2009, 01:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.