Results 1 to 2 of 2

Thread: QTransform rotation and scale issue

  1. #1
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QTransform rotation and scale issue

    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

    Qt Code:
    1. QTransform scale= transform();
    2. scale.translate( boundingRect().topLeft().x(), boundingRect().topLeft().y());
    3. scale..scale(2, 4);
    4. scale.translate( -boundingRect().topLeft().x(), -boundingRect().topLeft().y());
    To copy to clipboard, switch view to plain text mode 
    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

    Qt Code:
    1. QTransform rot= transform();
    2. rot.translate( boundingRect().center().x(), boundingRect().center().y());
    3. rot.rotate( angle);
    4. rot.translate( -boundingRect().center().x(), -boundingRect().center().y());
    To copy to clipboard, switch view to plain text mode 

    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.
    issue.jpg

    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.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTransform rotation and scale issue

    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)

    Qt Code:
    1. void GraphicsPathItem::keyPressEvent(QKeyEvent *e)
    2. {
    3. if (e->key() == Qt::Key_A) {
    4.  
    5. qDebug() << "Rotating item";
    6.  
    7. QTransform itTransf = transform();
    8. QPointF dp = this->boundingRect().center();
    9.  
    10. itTransf.translate( dp.x(), dp.y());
    11. itTransf.rotate( rotation() + 5, Qt::ZAxis);
    12. itTransf *= QTransform::fromScale( scale(), scale());
    13. itTransf.translate( -dp.x(), -dp.y());
    14.  
    15. setTransform(itTransf);
    16.  
    17. qDebug() << "Current transform:" << this->transform();
    18. }
    19.  
    20. if (e->key() == Qt::Key_S) {
    21.  
    22. qDebug() << "Scale item";
    23.  
    24. QTransform itTransf = transform();
    25. QPointF dp = this->boundingRect().center();
    26.  
    27. itTransf.translate( dp.x(), dp.y());
    28. itTransf *= QTransform::fromScale( scale() + 1.1, scale() + 2.5);
    29. itTransf.translate( -dp.x(), -dp.y());
    30.  
    31. setTransform(itTransf);
    32. qDebug() << "Current transform:" << this->transform();
    33. }
    34.  
    35. if (e->key() == Qt::Key_R) {
    36.  
    37. qDebug() << "Reset transform";
    38.  
    39. QTransform itTransf = transform();
    40.  
    41. itTransf.reset();
    42.  
    43. setTransform(itTransf);
    44. qDebug() << "Current transform:" << this->transform();
    45. }
    46.  
    47. return QGraphicsPathItem::keyPressEvent( e);
    48. }
    To copy to clipboard, switch view to plain text mode 

    Result:

    sc_rot.jpg

    I prepared compilable example to illustrate problem. testItemRotationScale.zip
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

Similar Threads

  1. Replies: 0
    Last Post: 28th September 2011, 23:18
  2. QTransform for walls
    By daorus in forum Qt Programming
    Replies: 3
    Last Post: 2nd August 2011, 23:25
  3. QTransform Pixel Issue
    By meazza in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 10:09
  4. QTransform rotate and scale order
    By qlands in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2010, 13:06
  5. QTransform()
    By mkind in forum Newbie
    Replies: 0
    Last Post: 19th February 2010, 21:45

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.