You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Tuesday, December 14th 2010, 8:52am

QGraphicsPixmapItem rotate/scale around center of pixmap

Greetings,
I have a problem with rotation and scale of QGraphicsPixmapItem. The center of rotation/scale is top left corner of pixmap. I would like to move the center of rotation/scale to center of pixmap. I have tried with translate(x,y).scale(sx,sy).translate(-x,-y), but this does not work well with scenePos. When I use setPos the new position is not correct (I saved scene position of the item into file and then I load it again). I have also tried to replace this with setTransformOriginPoint(x,y) but it works like I did nothing(center of scale is still top left corner). What am I doing wrong? Please help. It is driving me insane.
Thank you.

Regards,
MadBear

Loko Luke

Beginner

  • "Loko Luke" is male

Posts: 42

Location: Scotland

Occupation: Software Engineer

  • Send private message

2

Tuesday, December 14th 2010, 11:51am

Hi MadBear,

Would you be able to put up the code of where you create the pixmap item and where your setting it's position.

3

Wednesday, December 15th 2010, 7:27am

Greetings,

Well it is prety simple. I will try to provide basic example of what I am trying to achieve.
1. Creation of item

Source code

1
2
3
QGraphicsPixmap *pix=new QGraphicsPixmap();
pix->setPixmap(somePixmap);
scene->addItem(pix);


2. Mouse wheel event on scene

Source code

1
2
3
resetTransform();
QRectF brc=boundingRect();
setTransform(QTransform::fromTranslate(-brc.width()/2.0, -brc.height()/2.0)*QTransform::fromScale(sx, sy).rotate(alpha)*QTransform::fromTranslate(brc.width()/2.0, brc.height()/2.0), true);


3. Do something with scene position of item

Source code

1
2
3
4
5
6
7
    QList<QGraphicsItem*> sel=selectedItems();
    int i;
    for(i=0;i<sel.count();i++)
    {
        QPointF xy=sel[i]->scenePos();
        sel[i]->setPos(xy.x(),xy.y()-tranStep);    //here the position should only change for tranStep but it does not (the change is much larger)
    }


I hope that this explains basic idea of what I would like to do and where is the problem. Please help. Like I said it is driving me insane.
Thank you.

Regards,
MadBear

Loko Luke

Beginner

  • "Loko Luke" is male

Posts: 42

Location: Scotland

Occupation: Software Engineer

  • Send private message

4

Wednesday, December 15th 2010, 4:28pm

Hi MadBear,

Try setting the position of the pixmap when it is first created also.


QGraphicsPixmap *pix=new QGraphicsPixmap();
pix->setPixmap(somePixmap);
pix->setPos(100,100); // or pix->setPos(event.scenePos()) if you want to use a mouse click event
scene->addItem(pix);

I think the problem is that your pixmap does not have a position set orignally.

Also try use translate(0, - tranStep) instead of setpos again.