QGraphicsItem 在场景中返回错误的位置。 (Qt4.7.3)
我在 QGraphicsScene 中有一个 QGraphicsRectItem 项目。 该项目可移动并且没有父级。 我将项目从文件中读取位置并调用构造函数:
item = new QGraphicsRectItem (rect);
这有效。位置如预期。
从项目中获取位置来将位置存储回文件
item->pos().toPoint()
然后我尝试通过使用“位置错误 - 不是场景中的绝对位置” 。该位置相对于创建项目的最后位置。
pos() 是检索场景中项目位置的正确方法吗?
感谢您的任何提示!
附: scenePos() 返回相同的值
I have a QGraphicsRectItem item in a QGraphicsScene.
The item is movable and has no parent.
I place the item reading positions from file and calling the constructor:
item = new QGraphicsRectItem (rect);
that works. Positions are as intended.
Then I try to store position back to file by getting it from the item using
item->pos().toPoint()
The position is wrong - not the absolute position in scene. The position is relative to the last position, where the item was created.
Is pos() right method for retrieving the item position within the scene?
Thank you for any hints!
P.S.:
scenePos() returns the same values
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也遇到了 QGraphicsRectItem 的问题。我当前的假设是这样的:
您设置/获取的“矩形”是相对于项目的“位置”的,因此您可以使用其中之一(或者如果您想混淆则两者都使用)来控制几何体的位置实际上是画出来的。
我的解决方案是这样的:
I've had problems with QGraphicsRectItem as well. My current hypothesis is this:
The "rect" that you set/get is relative to the item's "pos", so you can use one or the other (or both if you want to get confused) to control the location at which the geometry is actually drawn.
My solution was this:
我发现你的问题是因为我正在网上搜索使用两者的提示。
我查看了 Qt 4.7.4 源代码:
QGraphicsRectItemPrivate
有一个成员QRectF rect
,您可以使用 rect() 获取它并使用 setRect() 进行更改。QGraphicsItem
,因此它也有一个QRectF pos
。boundingRect()
返回按画笔宽度调整的rect
,这意味着要更新的区域大体是正确的。所以,正如 aldo 所说,你必须决定是使用 rect.topLeft 还是 pos 进行定位。
为了帮助您和其他人做出决定,这里有一些优点和缺点:
使用 rect 的优点:
使用 rect 的缺点:
QGraphicsItem::ItemPositionChange
的函数。对我来说,只要您不使用 QGraphicsLineItem(如果我需要 QGraphicsLineItem,我可能会从中派生并自己发送 QGraphicsItem::ItemPositionChange),这一点就比优点更重要。如何以及何时使用两者:
如果您同时使用 pos 和 rect,则让 pos 成为 GraphicsItem 的中心。
想象一下图形场景中具有特定半径的标记、光标或类似物。如果你想使用 QGraphicsRectItem 来实现这一点,我建议:
这样, pos 总是指向中心,而 rect 仅由半径定义。
I found your question because I was searching the web on hints which of both to use.
I took a look at the Qt 4.7.4 sources:
QGraphicsRectItemPrivate
has a memberQRectF rect
which you can get with rect() and change with setRect().QGraphicsItem
, it also has aQRectF pos
.boundingRect()
returnsrect
adjusted by pen width, which means that the area to be updated is generally correct.So, as aldo said, you have to decide if you use rect.topLeft or pos for positioning.
To help you and others decide, here are a few pro and contra points:
Advantages of using rect:
QGraphicsLineItem
, the pos() function always points to the topLeft corner of the boundingRect, which doesn't have to be one of the line points, so by leaving pos at QPointF(0,0), the QLine points are always relative to the scene.Disadvantage of using rect:
QGraphicsItem::ItemPositionChange
. To me, this point weighs more than the advantages, as long as you're not using a QGraphicsLineItem (If I'd need a QGraphicsLineItem, I'd probably derive from it and send the QGraphicsItem::ItemPositionChange myself).How and when to use both:
If you use both pos and rect, let pos be the center of your GraphicsItem.
Imagine a marker, cursor or similar with a specific radius in your graphics scene. If you want to use
QGraphicsRectItem
for that, I suggest:This way, the pos always points to the center and the rect is only defined by the radius.
现在我找到了解决方法。如果是Qt的bug或者我对场景/视图的误解,请不要纠结。
现在,我在位置 0,0 创建项目,并使用 moveBy(x,y) 将它们移动到所需位置。
不知何故,很奇怪。
Now I found the workaround. Don't knot if it is bug of Qt or my misunderstanding on scene/view.
Now I create the items at the position 0,0 and move them to desired position using moveBy(x,y).
Weird, somehow.