gpt4 book ai didi

c++ - 为什么 QGraphicsItem::scenePos() 不断返回 (0,0)

转载 作者:可可西里 更新时间:2023-11-01 18:12:15 33 4
gpt4 key购买 nike

我一直在研究这段代码:

QGraphicsLineItem * anotherLine = this->addLine(50,50, 100, 100);
qDebug() << anotherLine->scenePos();

QGraphicsLineItem * anotherLine2 = this->addLine(80,10, 300, 300);
qDebug() << anotherLine2->scenePos();

this 指针指向一个QGraphicsScene。在这两种情况下,我都得到了 QPointF(0,0) 的输出。
通过阅读文档,我认为 scenePos() 应该返回位置线在场景中的位置,而不是它在局部坐标系中的位置。我做错了什么?

最佳答案

addLine 上仔细阅读 QT 4.5 文档后,我意识到我做错了什么。根据文档:

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0)

所以如果我指定 addLine(50,50, 100, 100),我实际上是在修改它的本地项目坐标。我所做的将其视为场景坐标的假设是错误的或没有根据的。我应该做的是这个

// Create a line of length 100
QGraphicsItem * anotherLine = addLine(0,0, 100, 100);

// move it to where I want it to be within the scene
anotherLine->setPos(50,50);

因此,如果我通过在场景中绘制来添加一条线,我需要将其中心重置为 (0,0),然后使用 setPos() 将其移动到我希望它在场景中的位置。

希望这对遇到同样问题的任何人有所帮助。

关于c++ - 为什么 QGraphicsItem::scenePos() 不断返回 (0,0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1151849/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com