为什么QGraphicsItem :: scenePos()保持返回(0,0) [英] Why does QGraphicsItem::scenePos() keep returning (0,0)

查看:923
本文介绍了为什么QGraphicsItem :: scenePos()保持返回(0,0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩这段代码:

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

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

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

解决方案

仔细阅读了添加Line ,我意识到我一直做错了。根据文档:


请注意,项目的几何体是在项目坐标中提供的
,其
位置是初始化为(0,0)


所以如果我指定addLine(50,50,100,100)局部项坐标。我假设它将被视为场景坐标是错误的或毫无根据的。我应该做的是这

  //创建一个长度为100的行
QGraphicsItem * anotherLine = addLine ,0,100,100);

//将它移动到我想要它在场景中的位置
anotherLine-> setPos(50,50);

所以如果我通过在场景中绘制来添加一行,我需要将它的中心重置为0,0)然后使用setPos()将它移动到我想要它在场景中的位置。



希望这可以帮助任何绊倒同样的问题。 / p>

I have been toying with this piece of code:

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

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

Where the this pointer refers to a QGraphicsScene. In both cases, I get QPointF(0,0) for both output.
From reading the document, I thought scenePos() is supposed to return the position of the line within the scene, not where it is within its local coordinate system. What am I doing wrong?

解决方案

After reading the QT 4.5 documentation carefully on addLine, I realize what I have been doing wrong. According to the doc:

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

So if I specify addLine(50,50, 100, 100), I am actually modifying its local item coordinate. The assumption I made that it will be treated as a scene coordinate is wrong or unfounded. What I should be doing is this

// 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);

So if I am adding a line by drawing within the scene, I need to reset its centre to (0,0) then use setPos() to move it to where I want it to be in the scene.

Hope this helps anyone who stumble upon the same problem.

这篇关于为什么QGraphicsItem :: scenePos()保持返回(0,0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆