2010-11-27 88 views
0

假设我有一个QGrahicsRectItem item item.width = 10和item.height = 10。它的左上角在(0,0)。 item.BoundingRect()应返回RectF(0,0,9,9)而是它返回一个RectF(0,0,10,10)QGraphicsRectItem :: boundingRect()返回一个比它应该大1px的QRectF

您可以用下面的代码进行测试:

QGraphicsRectItem* item = new QGraphicsRectItem(0,0,10,10); 
qDebug() << item->boundingRect().width();   // 10, OK 
qDebug() << item->boundingRect().height();   // 10, OK 
qDebug() << item->boundingRect().topLeft().x();  // 0, OK 
qDebug() << item->boundingRect().topLeft().y();  // 0, OK 
qDebug() << item->boundingRect().bottomRight().x(); // 10, WHY? should be 9 
qDebug() << item->boundingRect().bottomRight().y(); // 10, WHY? should be 9 

所以boundingRect()返回的宽度和11像素高度RectF,虽然宽度( )和身高()声称这两个都是10。

怎么了?

回答

0

我没有看到任何错误的。我想像是这样的(取代3为3)

0 1 2 3 
0 |==|==|==| 
    | 1| 2| 3| 
1 |==|==|==| 
    | 1| 2| 3| 
2 |==|==|==| 
    | 1| 2| 3| 
3 |==|==|==| 

它有宽度和高度3,左下角是[3,3],是吗? :)

编辑:

Here你可以看到它是如何实际计算的(注意,是它的计算方法不同,查阅QRect为和QRectF由于历史的原因(这就是文档要求))。

QRect坐标系和QRectF坐标系。

+0

但是RectF仍然是11px宽而不是10.这不是一个透视问题,而是一个导致问题的事实。这才是重点。如果我使用boundingRect()来捕捉我的10px项目中的鼠标事件,那么我还将获得与该项目相距1px的事件。 – problemofficer 2010-11-28 00:20:34

0

我想你在这里混淆坐标与数组。以上结果是正确的。

如果你离墙10米远转身去测量距离,你应该在10米远的地方,对吗?不是9--因为在进行第一步之前,你无法进入墙内。这也意味着墙壁厚1米。

相关问题