作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我一直在研究这段代码:
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/
我一直在研究这段代码: QGraphicsLineItem * anotherLine = this->addLine(50,50, 100, 100); qDebug() scenePos(); Q
我是一名优秀的程序员,十分优秀!