Results 1 to 6 of 6

Thread: Rotating a rectangle about its origin

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Rotating a rectangle about its origin

    hi
    How to rotate the rectangle about its origin. i have tried first by rotating the matrix and the calculating the distance between the topLeft() corner of a rectangle and the distance to which it has rotated ,and then translating it.I am unable to rotate about origin of rectangle, But i am able to rotate around the topLeft() corner of the rectangle.

    Here is some code which rotates the about the topLeft() of a rectangle .Please help modify the code to make the rectangle rotate around its centre.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Widget : public QWidget
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. void paintEvent(QPaintEvent * event)
    9. {
    10. QMatrix matrix;
    11. QRect rect(20,20,150,100);
    12. matrix.translate(rect.left(), rect.top());
    13. static int degree=0;
    14. matrix.rotate(++degree);
    15.  
    16. QPainter painter(this);
    17. painter.setMatrix(matrix);
    18. rect.setX(0);rect.setY(0);
    19. painter.drawRect(rect);
    20. painter.drawPoint(rect.center());
    21.  
    22. QTimer::singleShot(100,this, SLOT(update()));
    23. }
    24. };
    25.  
    26. int main(int argc, char *argv[])
    27. {
    28. QApplication a(argc, argv);
    29. a.setStyleSheet("QWidget{background: white}");
    30. Widget w;
    31. w.show();
    32. return a.exec();
    33. }
    34.  
    35. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by babu198649; 21st August 2008 at 12:25.

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
  •  
Qt is a trademark of The Qt Company.