PDA

View Full Version : coordinate problem



sai
17th April 2009, 08:54
I encounter a coordinate problem when I reimplement the QGraphicsView.

I derive from the QGraphicsView, to get the cursor position in the pixmap coordinate, which is on the scene( at center, i guess, cause I simply scene()->addPixmap(..) ).

void MyView::mousePressEvent (QMouseEvent *event)
{
...
QPointF cp = mapToScene(event->pos());
...
}

As I use the following codes to check the pixmap's location in scene coordinate
QGraphicsPixmapItem *item = (QGraphicsPixmapItem *)scene()->itemAt(0, 0);
QPointF offset1 = item->offset();
QPointF scePos = item->scenePos();

both offset1 and scePos is (0, 0)
So I think that the item is at scene's center, while the pixmap on it is at the item's center too.
Then the cp from the mousePressEvent should equal to its coordinate in pixmap, right ?
However, tha fact seems incorrect, could you give me any advice, 3ks.

wysota
17th April 2009, 11:21
The item is at scene's origin, not its centre (unless they are equal). The view (widget), the viewport (child of the view), the scene and the items all have different coordinate systems so you can't just take a position in the view's coordinate system and use it with methods that expect one of the other coordinate systems. If you want to do that, you have to map between the systems with appropriate methods available throughout the graphics view API.