设置qgraphicsitem原点,旋转后QGraphicsSvgItem定位

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:

7afbdf9fc3ed37c1481a80b1ad2a178b.png

But I got the following image:

18891e5593a886b65529a1dac0213110.png

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值