Results 1 to 3 of 3

Thread: How to make an QGraphicsObject animation?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2011
    Location
    Paraguay
    Posts
    5
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Exclamation How to make an QGraphicsObject animation?

    Hi,

    I am new on this forum.

    I am making an application. it consits of:
    1 QGraphicsView1 QGraphicsScene1 QGraphicsObject derived class.

    the derived class has emits a signal when clicked, an the application goes to another state, but before it goes, it has to first animate the QGrapchisObject derived class,
    that is inside the scene.

    I have a list of images, and I have to animate the sequence without erasing the first image.

    I think about creating a class to do the work.

    Qt Code:
    1. #ifndef GRAPHIC_H
    2. #define GRAPHIC_H
    3.  
    4. #include <QGraphicsObject>
    5. #include <QtCore>
    6.  
    7. class Graphic : public QGraphicsObject
    8. {
    9. Q_OBJECT
    10. protected:
    11. QPixmap image;
    12.  
    13. public:
    14. enum {Type = GraphicType};
    15. int type() const { return Type;}
    16.  
    17. Graphic(const QPixmap &pix,
    18. const QPointF & point = QPointF(),
    19. QGraphicsItem *parent = 0);
    20.  
    21. signals:
    22. void clicked();
    23. };
    24.  
    25. #endif // GRAPHIC_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #ifndef ANIMATIONHELPER_H
    2. #define ANIMATIONHELPER_H
    3.  
    4. #include <QObject>
    5.  
    6. class Graphic;
    7.  
    8. class AnimationHelper : public QObject
    9. {
    10. Q_OBJECT
    11. QList<Graphic *> m_sequence;
    12. QGraphicsScene *m_scene;
    13.  
    14. public:
    15. explicit AnimationHelper(QObject *parent = 0);
    16. explicit AnimationHelper(int pause, int duration, QGraphicsScene *scene, QObject *parent = 0);
    17. int m_pause;
    18. int m_duration;
    19. bool empty;
    20. public:
    21. AnimationHelper(QGraphicsScene *scene, QObject *parent = 0);
    22. void addGraphic(Graphic * myG);
    23.  
    24. public slots:
    25. void onGraphicClicked();
    26.  
    27. signals:
    28. void clicked();
    29.  
    30. };
    31.  
    32. #endif // ANIMATIONHELPER_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //implementation
    2.  
    3. MyAnimationHelper::onMyGraphicsClicked()
    4. {
    5. QSequentialAnimationGroup *animationGroup = new QSequentialAnimationGroup(this);
    6. animationGroup->addPause(m_pause;);
    7. foreach(Graphics *g, m_sequence)
    8. {
    9. scene->addItem(g);
    10. QPropertyAnimation *animation = new QPropertyAnimation(g, "opacity", this);
    11. animation->setDuration(m_duration;);
    12. animation->setStartValue(qreal(0));
    13. animation->setEndValue(qreal(1));
    14. animationGroup->addAnimation(animation);
    15. }
    16. animationGroup->start(QAbstractAnimation::DeleteWhenStopped);
    17.  
    18. emit clicked();
    19. }
    20.  
    21. #include <QSequentialAnimationGroup>
    22. #include <QPropertyAnimation>
    23. #include "animationhelper.h"
    24. #include "graphic.h"
    25.  
    26. AnimationHelper::AnimationHelper(QObject *parent) :
    27. QObject(parent)
    28. {
    29. }
    30.  
    31. AnimationHelper::AnimationHelper(int pause, int duration, QGraphicsScene *scene, QObject *parent) :
    32. QObject(parent), m_pause(pause), m_duration(duration), empty(true)
    33. {
    34. m_scene = scene;
    35. }
    36.  
    37. void AnimationHelper::addGraphic(Graphic * myG)
    38. {
    39. if(empty)
    40. {
    41. empty = false;
    42. myG->setProperty("opacity", 1);
    43. m_scene->addItem(myG);
    44. connect(myG, SIGNAL(clicked()), this, SLOT(onGraphicClicked()));
    45. }
    46. else
    47. {
    48. myG->setProperty("opacity", 0);
    49. m_scene->addItem(myG);
    50. m_sequence.append(myG);
    51. }
    52. }
    53.  
    54. void AnimationHelper::onGraphicClicked()
    55. {
    56. QSequentialAnimationGroup *animationGroup = new QSequentialAnimationGroup(this);
    57. animationGroup->addPause(m_pause);
    58. foreach(Graphic *g, m_sequence)
    59. {
    60. QPropertyAnimation *animation = new QPropertyAnimation(g, "opacity", this);
    61. animation->setDuration(m_duration);
    62. animation->setStartValue(qreal(0));
    63. animation->setEndValue(qreal(1));
    64. animationGroup->addAnimation(animation);
    65. }
    66. animationGroup->start(QAbstractAnimation::DeleteWhenStopped);
    67. emit clicked();
    68. }
    To copy to clipboard, switch view to plain text mode 

    I think you could get the idea, this actually works, but the main thing is that I dont know if there is a better way of making the animation.

    Best Regards.
    Last edited by wysota; 25th August 2011 at 07:52. Reason: missing [code] tags

Similar Threads

  1. protected QGraphicsObject
    By stefan in forum Qt Programming
    Replies: 6
    Last Post: 19th August 2011, 17:04
  2. Replies: 1
    Last Post: 28th July 2011, 11:28
  3. Replies: 1
    Last Post: 20th July 2011, 08:27
  4. Replies: 0
    Last Post: 5th January 2011, 18:17
  5. Replies: 1
    Last Post: 24th October 2010, 02:40

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