旋转后QGraphicsSvgItem定位 [英] QGraphicsSvgItem positioning after rotation
问题描述
当我尝试旋转QGraphicsSvgItem
对象并设置位置时,遇到了一个奇怪的问题/功能.
I encountered a strange issue/feature when I try to rotate a QGraphicsSvgItem
object and set a position.
检查以下示例:
为了简单起见,我有一个.svg
,它是一个蓝色矩形.我使用该文件创建了两个QGraphicsSvgItem
,并将它们旋转90度.我将它们都设置在相同的位置,但是旋转的一个被错误地"定位(也许这是正确的行为).看起来x,y坐标(左上角)现在是右上角,仍然像没有旋转一样.从我的角度来看,这看起来很奇怪,我希望当我旋转并设置位置时,形状的第一个点"应该位于形状开始"的位置.
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() <<"Bubble pos: " << _bubble->pos();
qDebug() <<"Bubble boundindRect: " << _bubble->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() <<"Bubble2 boundindRect: " << bubble2->boundingRect();
qDebug() << "Bubble2 rect: " <<bubble2->sceneBoundingRect();
qDebug() <<"Bubble2 topleft: " << bubble2->sceneBoundingRect().topLeft();
DrawableSvgItem只是QGraphicsSvgItem的扩展,我创建了QGraphicsSvgItem以便将源路径绘制到该项中.目前我没有使用它.
DrawableSvgItem is only an extension of QGraphicsSvgItem, I created for drawing sourcepaths into the item. Currently I am not using it.
输出:
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围绕其变换原点旋转.默认情况下,气泡2的坐标为0,0.另外,bubble2->setPos()
将此点设置为_bubble的0,0,因此您只需将bubble2->setPos()
调整为所需的位置.您可以通过setTransformOriginPoint()
更改变换原点.
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()
.
修改5.11.2014:
edit 5.11.2014:
bubble2的左上角是在bubble2坐标中定义的.这些坐标不会通过项转换来更改,旋转后,项的左上角与以前相同.这一点由setPos()放置.因此,图2显示了正确的行为.您要放置在bubble(x,y)上的角是bubble2的左下角.要获得图片1,您只需在代码中添加1行: 要么将气泡2的最后一步向右移动
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)
或首先将气泡2的转换原点设置到底部左角
or as first step set transformation origin point of bubble2 to bottem left corner
bubble2->setTransformOriginPoint(0,117)
这篇关于旋转后QGraphicsSvgItem定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!