maryam
maryam

Reputation: 1505

how to animat a QPixmap in qt?

if possible animated a QPixmap?in my program I have a QPixmap (image of super Mario).i want to animate it.i mean it should go forward and jumping.it is hard for me to design shape of super Mario again and prefer to use of image.

#ifndef MYQGRAPHICOBJECT_H
#define MYQGRAPHICOBJECT_H
#include <QGraphicsObject>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QPixmap>
#include <QPropertyAnimation>
extern QPixmap super;
class MyQgraphicObject : public QGraphicsObject
{
Q_OBJECT
private:
 QPropertyAnimation* pro;
 QPixmap pi;
 public:
explicit MyQGraphicObject(QGraphicsItem *parent = 0,QPixmap p= super);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QRectF boundingRect()const;
signals:
public slots:   
};
#endif // MYQGRAPHICOBJECT_H
#include "myqgraphicobject.h"
#include <QGraphicsItem>
MyQGraphicObject::MyQGraphicObject(QGraphicsItem *parent, QPixmap p) :
QGraphicsObject(parent),pi(p)
{
 pro=new QPropertyAnimation();
}
QRectF MyQGraphicObject::boundingRect() const
{
}

Upvotes: 1

Views: 2100

Answers (1)

Kunal
Kunal

Reputation: 3535

I have some code which does the same with sprite sheet. Its explained here

Basically, you draw picture with size and location of sprite

mSpriteImage = new QPixmap(":dragon.png");
painter->drawPixmap ( mPos.x(),mPos.y(), *mSpriteImage, 
                                   mCurrentFrame, 0, 100,100 );

and on timer, you change the location of sprite.

void Sprite::nextFrame()
{
    //following variable keeps track which 
    //frame to show from sprite sheet
    mCurrentFrame += 100;
    if (mCurrentFrame >= 500 )
        mCurrentFrame = 0;
    mPos.setX( mPos.x() + 10 );
}

Upvotes: 2

Related Questions