I've wirriten a class which describes a piece for tetris-puzze.
Each Piece shoud have a rotation method:
class Piece : public QPixmap, public QGraphicsPixmapItem
{
public:
void rotateLeft();
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QRectF boundingRect() const;
private:
QPixmap pixm;
};
Piece::Piece(QPixmap & pixmap)
: QPixmap(pixmap),QGraphicsPixmapItem(pixmap)
{
pixm= pixmap;
}
void Piece::rotateLeft()
{
double pi = 3.14;
double a = pi/180 * (-90.0);
double sina =sin(a);
double cosa =cos(a);
QMatrix rotationMatrix(cosa,sina,-sina,cosa,0,0);
QPixmap pixmap = this->transformed(rotationMatrix);
pixm= pixmap;
}
void Piece:aint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawPixmap ( 0,0, pixm );
}
QRectF Piece::boundingRect() const
{
return QRectF(0,0,pixm.width(),pixm.height());
}
When I would like rotate piece I use this code:
QGraphicsScene *scene = new QGraphicsScene();
QList<Piece*> list = model->getPieces();
Piece * p = list.takeFirst();
scene->addItem(p);
p->rotateLeft();
scene->update();
p->rotateLeft();
scene->update();
When I run this program I shoud see a piece rotated twice in left direction, but I see rotated it only once. Why?
Bookmarks