PDA

View Full Version : Rotating pixmap



jano_alex_es
17th December 2010, 17:55
Hello, I want to rotate the pixmap of the QGraphicsPixmap item and show the item rotated. Rotating the item cause other sort of problems.

So far, I never had problems when rotating the pixmaps, but I don't know why now the rotation is not taking from the center and rotates the pixmap, it's like its rotated without the translation and I don't know why.



//m_pxOriginal is a copy of the original pixmap. Not rotated.
qreal rRadius = m_pxOriginal.width() / 2;

//Rotate the previous rotation - 10 degrees
m_iAngle -= 10;

QPixmap rotatedPix = m_pxOriginal .transformed(QTransform().translate(-rRadius, -rRadius).rotate(m_iAngle).translate(rRadius, rRadius));

m_pxItem->setPixmap(rotatedPix);


Anything strange? :S thanks!

SixDegrees
17th December 2010, 18:32
Step one: get rid of the multiple transformations all packed onto a single line. Do them one at a time, examine each step and isolate the problem.

jano_alex_es
18th December 2010, 10:51
The point is the rotation of the pixmaps. I think there is something wrong, because the rotation of the item works fine (unfortunatly, I do need to create the rotation feeling through the pixmap rotation, item rotation is not a possibility). Maybe there is a problem when transforming from/to QImage.


Item rotation


m_pxItem->setTransformOriginPoint(rRadius, rRadius);
m_pxItem->setRotation(m_iAngle);


http://www.youtube.com/watch?v=k1TZRTaWGG0


Pixmap rotation


QPixmap pix1 = m_pxOriginal.transformed(QTransform().translate(-rRadius, -rRadius));
QPixmap pix2 = m_pxOriginal.transformed(QTransform().rotate(m_iAn gle));
QPixmap rotatedPix = pix2.transformed(QTransform().translate(rRadius, rRadius));
m_pxItem->setPixmap(rotatedPix);
//Just rotating, without the traslation, shows the same bounding effect



http://www.youtube.com/watch?v=YKpOMPBhl5I

jano_alex_es
18th December 2010, 13:19
I was able to workaround the problem thanks drawing in a rotated QPainter.



QPixmap rotatedPixmap(m_pxOriginal.size());
QPainter* p = new QPainter(&rotatedPixmap);
QSize size = m_pxOriginal.size();
p->translate(size.height()/2,size.height()/2);
p->rotate(m_iAngle);
p->translate(-size.height()/2,-size.height()/2);
p->drawPixmap(0, 0, m_pxOriginal);
p->end();
delete p;
m_pxItem->setPixmap(rotatedPixmap);



The rotation works fine! BUT a grey rectangle is painted:


http://s3.subirimagenes.com:81/otros/previo/thump_5668643why.jpg

Why is it created? and more important, how can I remove it?

jano_alex_es
19th December 2010, 04:00
Well, finally solved with the QPainter aproximation.



//Please note this method takes 2 mseg to finish for a 20x20 pixmap.
QPixmap rotatedPixmap(m_pixOriginal.size());
rotatedPixmap.fill(QColor::fromRgb(0, 0, 0, 0)); //the new pixmap must be transparent.
QPainter* p = new QPainter(&rotatedPixmap);
QSize size = m_pxOriginal.size();
p->translate(size.height()/2,size.height()/2);
p->rotate(m_iAngle);
p->translate(-size.height()/2,-size.height()/2);
p->drawPixmap(0, 0, m_pxOriginal);
p->end();
delete p;
m_pxItem->setPixmap(rotatedPixmap);



Definitly there is a translation problem when rotating a QPixmap, I don't know if its because the conversion to/from QImage or the known small rotation (according the QtAssistan it's possible to solve it with QPixmap::trueMatrix, but It doesn't solve it in my code) but the pixmap rotation is an issue in Qt.