QGraphicsItem返回场景中的错误位置。(Qt4.7.3) [英] QGraphicsItem returns wrong position in scene. (Qt4.7.3)

查看:19
本文介绍了QGraphicsItem返回场景中的错误位置。(Qt4.7.3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在QGraphicsScene中有一个QGraphicsRectItem项。 该项目可移动并且没有父。 我从文件中放置项的读取位置并调用构造函数:

item = new QGraphicsRectItem (rect);

这很管用。职位与预期相符。

然后我尝试通过使用

从项目中获取位置来将位置存储回文件中
item->pos().toPoint()

位置错误--不是场景中的绝对位置。该位置相对于创建项目的最后一个位置。

pos()是检索场景中项目位置的正确方法吗?

谢谢您的提示!

附注: ScenePos()返回相同的值

推荐答案

我在使用QGraphicsRectItem时也遇到了问题。我目前的假设是:

您设置/获取的"rect"是相对于项目的"pos"的,因此您可以使用其中一个(如果您想搞混,则同时使用两个)来控制实际绘制几何图形的位置。

我的解决方案是:

// 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

这篇关于QGraphicsItem返回场景中的错误位置。(Qt4.7.3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆