Getting mouse coordinates from the QGraphicsView's referential
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
Code:
{
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.
Re: Getting mouse coordinates from the QGraphicsView's referential
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.
Re: Getting mouse coordinates from the QGraphicsView's referential
Quote:
Originally Posted by
wysota
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.