PDA

View Full Version : Creating a new QPixmap out of a rotated QGraphicsPixmapItem



Aondiar
15th March 2010, 04:41
Hi all,

I have set up a QGraphicsScene & QGraphicsView and added a QGraphicsPixmapItem that has a square bounding QRect. The center of the QPixmap is at the scene center.
i.e.



QGraphicsView* graphicsView;
QGraphicsScene* graphicsScene = new QGraphicsScene();
QPixmap pic = QPixmap(":/new/pic.jpg").scaled(QSize(100,100));
QGraphicsPixmapItem* bg = graphicsScene->addPixmap(pic);
bg->setPos(-50,-50);
graphicsView->setScene(graphicsScene);
graphicsView->fitInView(bg);


This allows the pixmap to fill the entire view so I don't see any whitespace. The problem comes when I rotate by calling rotate() on the QGraphicsView which contains pixmap;



int degrees;
graphicsView->rotate(degrees);


Where degrees is some value I get from the user.

The view rotates as I would expect, creating a "diamond" from the rotated square QPixmap. What I would like to do now is to make a square again by essentially taking the inset square of the image, and filling the graphicsScene with that new image.


I appreciate any ideas anyone has for how to do this.
Thank you.

prof.ebral
15th March 2010, 07:23
I code in Python, FYI. I would recommend creating brand new classes that are subclasses of the Scene and Item objects you need. This will allow you to create brand new instances of the objects. Something like this in python looks like:



class MyGraphicsScene(QtGui.QGraphicsScene)
def __init__(self, parent):
super(MyGraphicsScene, self).__init__()

..and..


class MyGraphicsItem(QtGui.QGraphicsPixmapItem)
def __init__(self, parent):
super(MyGraphicsItem, self).__init__()


Then the object is itself a class, and an object, GraphicsScene or PixmapItem, allowing you call it, create a second instance of it easly, and even add functions inside it so you can call those.

I know you are not coding in PyQt, but I thought it would help.

aamer4yu
15th March 2010, 10:03
You could also use

bg->setFlags(bg->flags() | QGraphicsItem::ItemIgnoresTransformations );
Hope that is what you need :)