PDA

View Full Version : Coordinate problem



AndyQT
7th November 2012, 20:30
Hello!

I have some problems with coordinates. I have a QGraphicsScene in a QGraphicsView and on the scene there is a QGraphicsItem. I retrieve the center coordinates of the bounding box

QPointF coords= this->mapToScene(this->boundingRect().center());
How can I match the coordinate system for the expression above to one of those for mouse click coordinates?:


void MyClass::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QPoint clickCoords = QCursor::pos();
QPointF globalPos = event->screenPos();
...


I'm sorry, this coordinate thing has been discussed so many times, but I can't figure it out.

Andy

Santosh Reddy
7th November 2012, 21:06
event->scenePos();

AndyQT
8th November 2012, 07:14
Thank you very much, you opened my eyes. Now I realise, that I have focused too much on solving it from the other side, I was looking for a way to get the item boundingbox center coordinates to translate to global coordinates(btw how would I do that?, I didn't find a matching map to global function). But of course, for my needs there is no difference if global or scene corrdinates, they just have to match! Thank you!

Santosh Reddy
8th November 2012, 07:44
QGraphcisScene coordinates connot be directly mapped to screen coordinates, as the on screen position of QGraphicsScene is taken care by QGraphicsView. So to get the screen coridinates of a point you have convert the point to QGraphicsSecen coordinates and then to screen codinates using the QGraphicsView which is displaying the QGraphicsScene.


extern QGraphicsView * view;
extern QGraphicsScene * scene;
extern QGraphicsItem * item;

view->setScene(scene);
scene->addItem(item);

QPointF point;
point = item->boundingRect().center();
point = item->mapToScene(point);
point = view->mapFromScene(point);

Note that without having a view there is no sense for screen/viewport/global coordinates.

AndyQT
8th November 2012, 09:10
Thank you very much for the detailed explanation. It seems so logical when you see it.
And of course, in my case I should also stick to the scene coordinates, so I don't need to care about the window position. In fact, all my problem started by choosing the wrong source for cursor position - QCursor::pos() instead of the cursor scenePos supplied by the mouse event. I simply took what google gave me first :-(.

Now everything is OK!