1

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) but 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?

problemofficer
  • 435
  • 4
  • 15

2 Answers2

1

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.

BastiBen
  • 19,679
  • 11
  • 56
  • 86
0

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.

Palmik
  • 2,675
  • 16
  • 13
  • But still the RectF is 11px wide and not 10. That's not a question of perspective but a fact that leads to problems. That's the point. If I use boundingRect() to catch mouse events in my 10px items then I will also get events, that are 1px away from the item. – problemofficer Nov 28 '10 at 00:20
  • What do you mean? Is it rendered 11px wide? Because the debug clearly says that it's 10px wide. So the boundingRect() is really 10px wide (and so is rect() in this case). – Palmik Nov 28 '10 at 09:19
  • A graphicsitem has a continuous coordinate system, not pixels. In real coordinates, the length of line that goes from 0 to 10 is 10, not 11. When the scene is rendered, the items need to be rastered again, of course, so you can't predict the exact size in pixels, and shouldn't rely on it. Try to use QGraphicsScene's mouse*Event handlers passing QGraphicsSceneMouseEvents. – Frank Osterfeld Nov 28 '10 at 10:04
  • The precise case is that I use the boundingRect() to check if a moveMouseEvent is still inside an object because it receives events even when you are outside the object. This check fails because for one pixel the cursor is outside the object but inside the boundingRect(). Because I access a QList by using the cursor coordinates this leads to out of bound errors. So here boundingRect() does not represent the actual item. That's the problem. – problemofficer Nov 28 '10 at 13:14
  • @problemofficer I know this question is old, but if you experienced mouse sensitivity 1px above and left of the item, this is due to a bug in Qt (version <= 4.8, I believe). [See here...](http://qt.gitorious.org/qt/qt/commit/15c14584199dc43e4a309fc331f3144009008128) – Martin Hennings Jul 30 '13 at 09:45