You are not logged in.

1

Monday, April 25th 2011, 3:53pm

Center a QGraphicsPixmapItem at the center of the Scene

Hallo everyone

I just started programming with QGraphics\View.
I have generated all necessary things like View, Scene, SceneRect(-1000, -1000, 2000, 2000).
After that I implemented two lines(QGraphicsLineItems) one horizontaly, one vertically to find the center of the scene.
So far it looks quite good
Next I generated was a QGraphicsPixmapItem with a 40x40 pixmap.
Unfortunately the center of the pixmap doesn't match the center of the the crossing lines (should be 0,0).
I had to use setOffset(5,5) to place the pixmap in the scene's center but if I rotate the Item it tumbles around the scene's center

How do I get the pixmap in the center of the scene with setPos(0,0) without using an offset(which obviously doesn't work)

regards
hubipet

MasterBLB

Intermediate

  • "MasterBLB" is male

Posts: 188

Location: Poland/Wrocław

  • Send private message

2

Monday, April 25th 2011, 6:42pm

Try this Brother:

Source code

1
2
QRect &r=yaItem->boundingRect();
yaitem->setPos(centerX-r.width()/2,centerY-r.height()/2);

where centerX/Y are calculated offsets of centrum of the scene;
There are 10 kinds of people.Those who do understand the binary code,and those who do not ;)