PDA

View Full Version : Problem with rotating QGraphicsItem



petrus
22nd January 2007, 03:45
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::paint(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?

aamer4yu
22nd January 2007, 05:03
QPixmap pixmap = this->transformed(rotationMatrix);
pixm= pixmap;
This might be the problem.... u r transforming this but storing transformed pixmap in pixm...
if u r using pixm for internal pixmap, apply the transformation on it...
hope this helps

petrus
22nd January 2007, 05:22
I've sovled that :D by simplifing this class. Now inherits only QGraphicsPixmapItem and don't contain pixm field. If you are interested in I can present it, but now I'm to tired :p
Thanks for your help.