qgraphicem在场景中的位置

QGraphicsItem position in the scene

本文关键字:位置 在场 qgraphicem      更新时间:2023-10-16

我正在MAC OS X 10.9上使用QT 5.3 Widget应用程序,我遇到了一个问题。

在我的程序中,我有一个包含QGraphicsScene sceneQGraphicsView view,其中包含QGraphicsEllipseItem parent,我将其用作其他QGraphicsEllipseItem的父(以跟踪),让我们将最后一个椭圆称为child

现在,我想获得child相对于scene的位置。

每当我现在尝试获得场景中的坐标时,它都会返回[0,0]:

 QGraphicsItem * parent;
 QGraphicsScene * scene;
 ....
 parent = new QGraphicsEllipseItem(0,0,0,0);
 scene = new QgraphicsScene();
 scene.addItem(parent);
 ....
 new QGraphicsItem(12,12,10,10,parent);      //shown in the scene
 qDebug() << parent->childItems()[0].scenepos(); //returns [0,0]
 qDebug() << parent->childItems()[0].pos();      //returns [0,0]

如何获得物品相对于场景的坐标?

我已经阅读了一些关于这一点的教程,但我迷失在其中或不理解(英语不是我的母语)。

尝试使用setPos()函数设置项目的位置。

您用来获取场景位置的函数是正确的scenePos(),否则您也可以使用mapToScene()