PDA

View Full Version : Rotate QGraphicsPixmapItem



durbrak
15th April 2007, 13:52
Okay, I've been sitting on this issue for at least 4 hours now and just am exhausted.
I'm trying to simply rotate a QGraphicsPixmapItem by 45° around its own axis.


MyPixmap::MyPixmap() : QGraphicsPixmapItem()
{
this->setPixmap(QPixmap("wallpaper.jpg"));

QTransform t = this->transform();

t.translate(this->pixmap().width()/2, this->pixmap().height()/2);
t.rotate(45, Qt::XAxis);
t.translate(-this->pixmap().width()/2, -this->pixmap().height()/2);

this->setTransform(t);
}I tried alot of other options but the image is always moving around the whole screen and this is what comes out:

http://img292.imageshack.us/img292/5521/capturenm9.th.jpg (http://img292.imageshack.us/my.php?image=capturenm9.jpg)

Shouldn't the lower edge be large and the upper edge small, like a trapezoid? Anyway, that's what I want it to be but it seems like I'm missing something here :(

marcel
15th April 2007, 14:14
Why not use QGraphicsItem::rotate()?
You can also do translations with the QGraphicsPixmapItem::translate();

Be careful:
this->pixamp->width() and height() are in local item coordinates.
You should use boundingRect() and translate to scene coords, and make the rotation from the scene, not from the item...

durbrak
15th April 2007, 14:20
QGraphicsItem::rotate() only accepts one parameter (the degrees), so I can't find a way to tell it on which axis I want to rotate it on.

And from translate() doc:

Translates the current item transformation by (dx, dy).
So I think the local item coods are enough.

jpn
15th April 2007, 14:20
Maybe this article (http://labs.trolltech.com/blogs/2006/11/30/zoom-and-rotate-on-mouseover-effect-in-graphics-view/) will help?

durbrak
15th April 2007, 14:26
Thank you jpn, but I've already read that article and tried lots of examples from TT Labs but they all only work for the usual Z-Axis (which is not a problem and not really hard).

marcel
15th April 2007, 14:33
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.

Keyword: 2D.

I believe you'll have to do more computations than that to get a rotation around X. Like obtaining a projection on XY plane of the rotated image rect and then map the image on the resulting shape. Then you'll get a trapezoid.

durbrak
15th April 2007, 14:48
Well, I know. But I assumed since it also just uses QPainter with QTransform that wouldn't be a problem.
TT Labs also created examples to achieve that:

http://labs.trolltech.com/images/e/e1/Qtransform.png
http://labs.trolltech.com/images/4/43/Qtransform2.png
http://labs.trolltech.com/images/d/db/Browser.png
I looked into it but they all don't use QGraphicsView, they just use QWidget and paint the items on it with QPainter and QTransform (with similar code to mine above).

marcel
15th April 2007, 14:51
Are you sure they don't do any extra things to the pixmaps once they have the transforms?