I encountered a strange issue/feature when I try to rotate a QGraphicsSvgItem object and set a position.
Check the following example:
I have one .svg for the sake of simplicity that is a blue rectangle. I create two QGraphicsSvgItem using the file and I rotate on of them with 90 degree. I set both of them to the same position, however the rotated one positioned 'wrongly'(maybe that is the correct behaviour). It looks like that the x,y coordinate(top-left corner) is now the top-right corner, remains like that it is not rotated. It looks strange from my viewpoint, I expect that when I rotate and set the position the 'first point' of the shape should be where the shape 'starts'.
I hope with the source code is more understandable that I wrote above:
SVGDom *bubbleDom = shadowgram->bubble();
_bubble = new DrawableSvgItem(sh->message(), bubbleDom->byteArray());
this->addItem(_bubble);
_bubble->setPos((this->width()-_bubble->sceneBoundingRect().width()) / 2, (this->height() - _bubble->sceneBoundingRect().height()) / 2);
qDebug() <pos();
qDebug() <boundingRect();
qDebug() << "Bubble rect: " <<_bubble->sceneBoundingRect();
qDebug() << "Bubble topleft: " <<_bubble->sceneBoundingRect().topLeft();
DrawableSvgItem *bubble2 = new DrawableSvgItem(sh->message(), bubbleDom->byteArray());
this->addItem(bubble2);
bubble2->setRotation(90);
bubble2->setPos(_bubble->pos());
qDebug() << "Bubble2 pos: " << bubble2->pos();
qDebug() <boundingRect();
qDebug() << "Bubble2 rect: " <sceneBoundingRect();
qDebug() <sceneBoundingRect().topLeft();
DrawableSvgItem is only an extension of QGraphicsSvgItem, I created for drawing sourcepaths into the item. Currently I am not using it.
Output:
Bubble pos: QPointF(413.5, 297)
Bubble boundindRect: QRectF(0,0 171x117)
Bubble rect: QRectF(296.5,297 117x171)
Bubble topleft: QPointF(296.5, 297)
Bubble2 pos: QPointF(413.5, 297)
Bubble2 boundindRect: QRectF(0,0 171x117)
Bubble2 rect: QRectF(411.458,297 173.016x119.967)
Bubble2 topleft: QPointF(411.458, 297)
In pictures, I expect the following:
But I got the following image:
I found a similar problem here.
解决方案
bubble2 is rotated around its transform origin point. That is by default 0,0 in the coordinates of bubble2. Additionally bubble2->setPos() sets this point to the 0,0 of _bubble, so you only need to adjust bubble2->setPos() to the wanted pos. You can change the transform origin point by setTransformOriginPoint().
edit 5.11.2014:
Top-left of bubble2 is defined in bubble2-coordinates. These coordinates are not changed by items transformations, after rotation the top-left corner of the item is the same corner as before. This point is placed by setPos(). So picture 2 shows the correct behaviour. The corner, you want to place on bubble(x,y), is bubble2's bottom-left corner. To get picture 1 you only need to add 1 line to your code:
either move bubble2 in last step by its height to the right
bubble2->setpos(413.5 + 117, 297)
or as first step set transformation origin point of bubble2 to bottem left corner
bubble2->setTransformOriginPoint(0,117)