PDA

View Full Version : Getting mouse coordinates from the QGraphicsView's referential



bshikari
27th March 2013, 23:50
I'm using Qt 4.8.4 on Qt Creator 2.0.1.

I have created a new widget to replace QGraphicsView on my mainwindow, in order to override wheelevents, mousepressevents, etc. I logically inherit QGraphicsView.

I'm using a mousemoveevent to show the coordinates on a label, also on the widget.

This is my code


void Graph::mouseMoveEvent(QMouseEvent *event)
{
QPointF position = event->posF();

ui->mouse_coordinates->setText("( " + QString::number((int)position.x())
+ ", " + QString::number((int)position.y())
+ " )");
}

But this always show the coordinates in the widget's window (being (0, 0) the top left corner). Because I use zoom and scroll bars on my reimplemented QGraphicsView, these absolute coordinates are useless, since I need this to identify points in a graph drawn in the View.

Is there a way of converting these coordinates to the QGraphicsView referential, i.e., for example, if I have a square drawn at the QGraphicsView (0, 0) (that's not located at the top left corner), how do I get the coordinates (0, 0) when my mouse hovers that square?

P.S.: I have tried this with mousepressevent as well with the same result

Thank you,
Bernardo M.

wysota
28th March 2013, 06:29
They already are in the view's coordinate space. What you want is the scene coordinate space. You can use QGraphicsView::mapToScene() for that or you can reimplement the mouse event directly in the scene.

bshikari
29th March 2013, 03:50
They already are in the view's coordinate space. What you want is the scene coordinate space. You can use QGraphicsView::mapToScene() for that or you can reimplement the mouse event directly in the scene.

Yes, I'm sorry, that's exactly what I meant.
I wiil try that. thank you.