# QGraphicsScene: Wrong coordinates returned for position&#63;

• I am intrigued by the following behavior when returning the position of a QGraphicsObject

Here is the code for three cases I tested:

@QGraphicsScene scene;
scene.setSceneRect(-(QApplication::desktop()->width()-10)/2, -(QApplication::desktop()->height()-10)/2, QApplication::desktop()->width()-10, QApplication::desktop()->height()-10);

// 1. This will draw a thick line starting from the center of the screen (at scene position 0,0)
Wall *topWall = new Wall(QRectF(0, 0, scene.sceneRect().width(), WallThickness));
qreal xx = topWall->x(); // The value of x is 0

// 2. This will draw a thick line at the top of the screen (at pos -315, -175)
Wall *topWall = new Wall(QRectF(0, 0, scene.sceneRect().width(), WallThickness));
topWall->setPos(scene.sceneRect().x(), scene.sceneRect().y());
qreal xx = topWall->x(); // The value of x is -315

// 3. This will draw a thick line at the top of the screen (at pos -315, -175)
Wall *topWall = new Wall(QRectF(scene.sceneRect().x(), scene.sceneRect().y(), scene.sceneRect().width(), WallThickness));
qreal xx = topWall->x(); // The value of x is 0 !!! Why?

Why is the value of x zero in the 3rd example? I guess the answer would be: because the position is not set. Then why is the line drawn at the right position?

Just a note:
scene.sceneRect().x() returns -315
scene.sceneRect().y() returns -175

And the implementation of Wall:
@Wall::Wall(QRectF aRectF, QGraphicsItem *parent)
: QGraphicsObject(parent)
, rect(aRectF)
{
}

Wall::~Wall()
{
}

QRectF Wall::boundingRect() const
{
}

QPainterPath Wall::shape() const
{
QPainterPath path;
return path;
}

void Wall::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QPen pen(niceBlue);
painter->setPen(pen);
painter->setBrush(niceBlue);
painter->drawRect(rect);
}

@

Thanks!

• I'm not an expert in this area. You posted in Mobile/Embedded but perhaps it isn't related to mobile?

It is because the coords are mapped to the wall. When you initialise the Wall with a QRectF, the x and y set the 'top-left' corner of the wall. In the mapping process, this is mapped as the 'origin' (0,0).

However, if you set top-left to (0,0) as you do in #2, it will be mapped as (0,0) at the top-left corner. So when you change the position to -315,-175, that will be the position of x,y.

It will still draw at the correct location, only the mapping is changed.

Read here for further details: http://doc.qt.nokia.com/latest/qrectf.html#QRectF-4

There is quite a lot of functions in QRectF to move either the mapping (won't affect x) or without mapping (does affect x).

Hope you understood that :) Tell me if you need clarification.

• [quote author="dflorin" date="1292623469"]Why is the value of x zero in the 3rd example? I guess the answer would be: because the position is not set.[/quote]

That is correct.

[quote author="dflorin" date="1292623469"] Then why is the line drawn at the right position?[/quote]

For the third Wall, you have set the rect to -315, -175. Coordinates for painting of the item are relative to the item's position (0, 0), and therefore match the scene coordinates.

• Got it. Thanks!

I posted it in Mobile and Embedded as I am developing for Symbian. Actually I could not find a generic, platform independent forum, as this message is not specific to Desktop / Mobile and Embedded. Maybe there should be a Development forum which is not platform oriented.

Thanks again.