我在QGraphicsScene中有一个QGraphicsRectItem项.该项目是可移动的,没有父项.我从文件中放置项目读取位置并调用构造函数:
item = new QGraphicsRectItem (rect);
Run Code Online (Sandbox Code Playgroud)
这样可行.职位如预期.
然后我尝试通过从项目中获取它来将位置存回文件
item->pos().toPoint()
Run Code Online (Sandbox Code Playgroud)
位置错误 - 不是场景中的绝对位置.该位置相对于创建项目的最后位置.
pos()是否正确检索场景中的项目位置?
谢谢你的任何提示!
PS: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
Run Code Online (Sandbox Code Playgroud)