Results 1 to 4 of 4

Thread: Rotating QGraphicsItem

  1. #1
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Rotating QGraphicsItem

    Hello,
    I want to rotate graphicsitems when I right click it. It should rotate progressively i.e for one click the angle with horizontal should be 90 deg, for 2nd click it should be 180(with horizontal) and so on.
    But it doesn't clear the area (occupied by it before rotation ) when the angle is 270 or 90 and then it is right clicked. It looks as though two items are present. I tried by adding calls to update manually, but in vain.
    What am I doing wrong ?
    Please help me solve this.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Rotor : public QGraphicsItem
    4. {
    5. public:
    6. Rotor(QGraphicsScene *s) : QGraphicsItem(0l,s)
    7. {
    8. setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
    9. }
    10.  
    11. void mousePressEvent(QGraphicsSceneMouseEvent * e)
    12. {
    13.  
    14. QRectF br = QRectF(mapToScene(boundingRect().topLeft()),boundingRect().size());
    15.  
    16. if(e->buttons() & Qt::RightButton)
    17. rotate(90);
    18. scene()->update(br);
    19. QGraphicsItem::mousePressEvent(e);
    20. }
    21.  
    22. void paint(QPainter *p,const QStyleOptionGraphicsItem * , QWidget *)
    23. {
    24. p->drawLine(-18, -9, 18, -9);
    25. p->drawLine( 18, -9, 18, 9);
    26. p->drawLine( 18, 9,-18, 9);
    27. p->drawLine(-18, 9,-18, -9);
    28. p->drawLine(-30, 0,-18, 0);
    29. p->drawLine( 18, 0, 30, 0);
    30. }
    31.  
    32. QRectF boundingRect() const
    33. {
    34. return QRectF(-30,-9,60,18);
    35. }
    36. };
    37. int main(int argc, char *argv[])
    38. {
    39. QApplication app(argc, argv);
    40.  
    41. QGraphicsScene scene(0,0,250,250);
    42. QGraphicsView* view = new QGraphicsView(&scene);
    43. view->setRenderHint(QPainter::Antialiasing);
    44.  
    45. Rotor *r = new Rotor(&scene);
    46. r->setPos(125,125);
    47. r = new Rotor(&scene);
    48. r->setPos(57,75);
    49. view->show();
    50.  
    51. return app.exec();
    52. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Rotating QGraphicsItem

    It works correctly for me on Linux with Qt4.2.2

  3. #3
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Rotating QGraphicsItem

    The workaround for < 4.2.2 is to call update also before the rotate.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  4. The following user says thank you to Bitto for this useful post:

    Gopala Krishna (21st December 2006)

  5. #4
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Rotating QGraphicsItem

    Thanks for the tip !

Similar Threads

  1. QGraphicsItem hover event
    By xgoan in forum Qt Programming
    Replies: 6
    Last Post: 4th January 2016, 15:08
  2. Rotating QGraphicsEllipseItem, but keeping its location
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 9th February 2012, 12:47
  3. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  4. QGraphicsItem problem - how to save items info ??
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 17th October 2006, 12:17
  5. (QT4.2-RC1) QGraphicsScene QGraphicsView QGraphicsItem
    By antonio.r.tome in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 10:56

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.