pixmap->fill(QColor("transparent"));painter->drawPixmap(0, 0, 1024, 640, *spriteImage, x,y,1024,640);
paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
#include "animatedsprite.h"AnimatedSprite::AnimatedSprite(QWidget *parent) : QWidget(parent), x(0), y(0){ pixmap = new QPixmap(128,128); pixmap->fill(QColor("transparent")); spriteImage = new QPixmap(":/img/images/sprite2.png"); painter = new QPainter(pixmap); label = new QLabel(this); label->setGeometry(0,0,128,128); label->show(); timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(nextSlide())); timer->start(30);}void AnimatedSprite::paintEvent(QPaintEvent *){ pixmap->fill(QColor("transparent")); painter->drawPixmap(0, 0, 1024, 640, *spriteImage, x,y,1024,640); label->setPixmap(*pixmap);}void AnimatedSprite::nextSlide(){ if(x > 895) { x = 0; if(y > 512) { y = 0; } else y += 128; } else { if(x >= 512 && y >= 512) { x = 0; y = 0; //timer->stop(); } x += 128; } this->update();}
protected:void advance(int phase);
timer = new QTimer(); connect(timer, SIGNAL(timeout()), &scene, SLOT(advance())); timer->start(30); PixmapItem* obj = new PixmapItem(); obj->setX(128); obj->setY(128); scene.addItem(obj);