开发者

QGraphicsRectItem::boundingRect() returns a QRectF that is 1px bigger than it should be

开发者 https://www.devze.com 2023-01-27 01:08 出处:网络
Let\'s say I have a QGrahicsRectItem item with item.width=10 and item.height=10. Its top left corner is at (0,0). item.BoundingRect() should return a RectF(0,0,9,9) 开发者_如何学Pythonbut instead it r

Let's say I have a QGrahicsRectItem item with item.width=10 and item.height=10. Its top left corner is at (0,0). item.BoundingRect() should return a RectF(0,0,9,9) 开发者_如何学Pythonbut instead it returns a RectF(0,0,10,10)

You can test it with the following code:

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

So boundingRect() returns a RectF that has a width and height of 11px, although width() and height() claim it's 10 for both.

What's wrong?


I think you confuse coordinates with arrays here. The above results are correct.

If you step 10 meters away from a wall and turn around to measure the distance, you should be 10 meters away, right? Not 9 - because you can't be inside the wall before making the first step. That would also imply that the wall is 1 meter thick.


I see nothing wrong with that. I imagine it like this (replace 10 for 3)

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

It has width and height 3 and the bottom left corner is at [3,3], is that right? :)

Edit:

Here you can see how it's actually calculated (notice, that it's calculated differently for QRect and QRectF for historical reasons (that is what docs claim)).

The QRect coordinate system and the QRectF coordinate system.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号