QGraphicsItem 在场景中返回错误的位置。 (Qt4.7.3)

发布于 2024-12-29 00:56:43 字数 415 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坦然微笑 2025-01-05 00:56:43

我也遇到了 QGraphicsRectItem 的问题。我当前的假设是这样的:

您设置/获取的“矩形”是相对于项目的“位置”的,因此您可以使用其中之一(或者如果您想混淆则两者都使用)来控制几何体的位置实际上是画出来的。

我的解决方案是这样的:

// Given QRectF scene_rect that represents the rectangle I want drawn...
setPos( scene_rect.topLeft() ); // Set the item's "position" relative to the scene
scene_rect.moveTo( 0.0, 0.0 );  // ...then set the rectangle's location to (0,0)
setRect( scene_rect );          // ...so the rectangle is drawn at (0,0) relative to the item

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:

// Given QRectF scene_rect that represents the rectangle I want drawn...
setPos( scene_rect.topLeft() ); // Set the item's "position" relative to the scene
scene_rect.moveTo( 0.0, 0.0 );  // ...then set the rectangle's location to (0,0)
setRect( scene_rect );          // ...so the rectangle is drawn at (0,0) relative to the item
风追烟花雨 2025-01-05 00:56:43

我发现你的问题是因为我正在网上搜索使用两者的提示。

我查看了 Qt 4.7.4 源代码:

  • QGraphicsRectItemPrivate 有一个成员 QRectF rect,您可以使用 rect() 获取它并使用 setRect() 进行更改。
  • 但是,由于它派生自QGraphicsItem,因此它也有一个QRectF pos
  • 函数boundingRect()返回按画笔宽度调整的rect,这意味着要更新的区域大体是正确的。

所以,正如 aldo 所说,你必须决定是使用 rect.topLeft 还是 pos 进行定位。
为了帮助您和其他人做出决定,这里有一些优点和缺点:

使用 rect 的优点:

  • 您可以在一个函数调用中定义 QGraphicsRectItem 的位置和大小。
  • 在 QGraphicsLineItem 的情况下, pos() 函数始终指向boundingRect的左上角,它不必是线点之一,因此将 pos 保留在 QPointF(0,0 ),QLine 点始终相对于场景。

使用 rect 的缺点:

  • 因为您无法使用 setPos(),所以没有发出 QGraphicsItem::ItemPositionChange 的函数。对我来说,只要您不使用 QGraphicsLineItem(如果我需要 QGraphicsLineItem,我可能会从中派生并自己发送 QGraphicsItem::ItemPositionChange),这一点就比优点更重要。

如何以及何时使用两者:

如果您同时使用 pos 和 rect,则让 pos 成为 GraphicsItem 的中心。
想象一下图形场景中具有特定半径的标记、光标或类似物。如果你想使用 QGraphicsRectItem 来实现这一点,我建议:

QPointF markerpos = GetMarkerPos();
double r = GetMarkerRadius();
QRectF markerrect(-r, -r, +r * 2., +r * 2.); // topleft: -radius, size: 2*radius
setPos(markerpos);
setRect(markerrect);

这样, 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 member QRectF rect which you can get with rect() and change with setRect().
  • But, as it is derived from QGraphicsItem, it also has a QRectF pos.
  • The function boundingRect() returns rect 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:

  • You can define the position and size of the QGraphicsRectItem in one function call.
  • In case of 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:

  • Because you can't use setPos(), there is no function that emits a 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:

QPointF markerpos = GetMarkerPos();
double r = GetMarkerRadius();
QRectF markerrect(-r, -r, +r * 2., +r * 2.); // topleft: -radius, size: 2*radius
setPos(markerpos);
setRect(markerrect);

This way, the pos always points to the center and the rect is only defined by the radius.

别忘他 2025-01-05 00:56:43

现在我找到了解决方法。如果是Qt的bug或者我对场景/视图的误解,请不要纠结。

现在,我在位置 0,0 创建项目,并使用 moveBy(x,y) 将它们移动到所需位置。

item = new QGraphicsRectItem( QRect(QPoint(x,y),QSize(w,h)) );
QPoint p = item->pos().toPoint(); //WRONG position! Relative to x,y

item = new QGraphicsRectItem( QRect(QPoint(0,0),QSize(w,h)) );
item->moveBy(x,y);
QPoint p = item->pos().toPoint(); //Right position! Relative to 0,0 of scene

不知何故,很奇怪。

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).

item = new QGraphicsRectItem( QRect(QPoint(x,y),QSize(w,h)) );
QPoint p = item->pos().toPoint(); //WRONG position! Relative to x,y

item = new QGraphicsRectItem( QRect(QPoint(0,0),QSize(w,h)) );
item->moveBy(x,y);
QPoint p = item->pos().toPoint(); //Right position! Relative to 0,0 of scene

Weird, somehow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文