Results 1 to 10 of 10

Thread: rotation of an animated QGraphicsItem

  1. #1
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default rotation of an animated QGraphicsItem

    hi there,

    i've subclassed a QGraphicsItem to create a rotating & clickable icon for a QGraphicsScene,

    rotation works fine, using QTimeLine & QGraphicsItemAnimation,

    the problem that I'm facing now is that my item rotates around the coordinates 0/0,

    how do I tell my item to rotate around its center: _with/2 / _height/2?

    regards

    ps: here's the code:
    Qt Code:
    1. #ifndef ANIMATEICON_H
    2. #define ANIMATEICON_H
    3.  
    4. #include <QObject>
    5. #include <QGraphicsItem>
    6. #include <QRectF>
    7. #include <QPainter>
    8. #include <QStyleOptionGraphicsItem>
    9. #include <QWidget>
    10. #include <QTimeLine>
    11. #include <QGraphicsItemAnimation>
    12.  
    13. class AnimateIcon : public QObject, public QGraphicsItem
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. AnimateIcon(QGraphicsItem *parent = 0, int width = 48, int height = 48) : QGraphicsItem(parent){
    19. _width = width;
    20. _height = height;
    21. _animation = new QGraphicsItemAnimation(this);
    22. _timeLine = new QTimeLine(10000, this);
    23.  
    24. _animation->setItem(this);
    25. _animation->setTimeLine(_timeLine);
    26. for (int i = 0; i <= 360; i+=18)
    27. _animation->setRotationAt(i/360.0, i);
    28.  
    29. _timeLine->setCurveShape(QTimeLine::LinearCurve);
    30. _timeLine->setLoopCount(0);
    31. _timeLine->start();
    32.  
    33. }
    34. ~AnimateIcon(void){}
    35. QRectF boundingRect(void) const{
    36. return QRectF(0, 0, _width, _height);
    37. }
    38. void paint(QPainter *painter, const QStyleOptionGraphicsItem * option, QWidget *widget = 0){
    39. painter->drawPixmap(0, 0, _width, _height,QPixmap(":/icons/action.png"));
    40. }
    41.  
    42. private:
    43. int _width;
    44. int _height;
    45. QTimeLine *_timeLine;
    46. };
    47. #endif
    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: rotation of an animated QGraphicsItem

    Make its bounding rect QRect(-_width/2, -_height/2, _width, _height) and draw it accordingly. Simply speaking make 0,0 the middle of the item, not the top left corner.

  3. The following user says thank you to wysota for this useful post:

    darksaga (6th September 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: rotation of an animated QGraphicsItem

    Quote Originally Posted by wysota View Post
    Make its bounding rect QRect(-_width/2, -_height/2, _width, _height) and draw it accordingly. Simply speaking make 0,0 the middle of the item, not the top left corner.
    already thought about doing it this way,

    but I thought, that there was some kind of function hidden inside the documentation I didn't find, something like setRotationAnchor()/setRotationCenter()

    anyway, thx for your suggestion

  5. #4
    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: rotation of an animated QGraphicsItem

    No, there isn't. A matrix rotation is always around the origin.

  6. #5
    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: rotation of an animated QGraphicsItem

    To rotate around an arbitrary point (x, y), or (width/2, height/2) in your case, do this:

    Qt Code:
    1. item->setTransform(QTransform().translate(x, y).rotate(a).translate(-x, -y));
    To copy to clipboard, switch view to plain text mode 

    To scale around an arbitrary point (x, y), or (width/2, height/2) in your case, do this:

    Qt Code:
    1. item->setTransform(QTransform().translate(x, y).scale(sx, sy).translate(-x, -y));
    To copy to clipboard, switch view to plain text mode 

    I guess you see the pattern ;-).
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  7. #6
    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: rotation of an animated QGraphicsItem

    I guess it's simpler to just move the origin

  8. #7
    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: rotation of an animated QGraphicsItem

    That depends on the item - sometimes it's easier to replace one line than to rewrite all your coordinates ;-).
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  9. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: rotation of an animated QGraphicsItem

    what about QGraphicsSvgItem. It already implements boundingRect() which returns 0,0 as Point of origin for one Item I'm testing here. Can I also handle that doing a transformation? Redefining boundingRect seemingly isn't an option here ...

    Thanx in advance

  10. #9
    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: rotation of an animated QGraphicsItem

    Quote Originally Posted by Bitto View Post
    That depends on the item - sometimes it's easier to replace one line than to rewrite all your coordinates ;-).
    You can always do the translation in the item's paint routine. Also a single line

  11. #10
    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: rotation of an animated QGraphicsItem

    momesana, you can just use the translate-rotate-translate trick from above, it works for any item and you don't have to modify it / subclass it / change its bounding rect or painting.

    wysota, you'd have to translate the bounding rect, the shape, and the painter inside the paint() function. otherwise you'd get painting and collision artifacts.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

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

    momesana (10th September 2007)

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31

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.