【发布时间】:2020-06-06 07:56:03
【问题描述】:
用Qapplication和QGraphicsItem Library来确定一个物体的坐标,通常使用setPos和setPos(mapToParent(yvelocity,-xvelocity))。这些命令的问题是它们决定了物体移动的“速度”(不是它们的XY坐标!) .
那么通过什么命令我可以提供 X 和 Y 输入并且对象会转到这些坐标?
提前致谢。
【问题讨论】:
-
您可以检索当前位置(使用pos())并通过分别添加
yvelocity和-xvelocity来设置新位置。 -
@Scheff,这是怎么做到的?可以举个例子解释一下,写个Commands吗?
-
类似
setPos(pos() + mapToParent(yvelocity,-xvelocity))。虽然pos()返回QPointF,但您可以应用operator+。 - 有合适的过载。你对向量代数有点熟悉吗? -
@Scheff,是的,非常感谢,我会测试一下。
-
@Scheff,这是不可能的。我的代码
QPointF location =this->pos(); xvelocity = .6*cos(head); yvelocity =.6*sin(head); setPos(location + mapToParent(yvelocity,-xvelocity));与此代码输出是:QPointF(3.37262,-2.16553) QPointF(7.04894,-4.19207) QPointF(7510.47,-4139.51) QPointF( 961316,-529883) QPointF(1.92263e+06,-1.05977e+06) QPointF(7.8751e+09,-4.3408e+09) QPointF(1.57502e+10,-8.6816e+09) QPointF(3.15004e+10) ,-1.73632e+10) &........
标签: qt qgraphicsscene qgraphicsitem qapplication