Is there a way to get mouse\'s coordinates on plotting area of a QChartView
? Preferably in the axis units. The goal is to display mouse\'s coordinates while mov
QChartView
is simply a QGraphicsView
with an embedded scene()
. To get coordinates within any of the charts, you have to go through several coordinate transformations:
view->mapToScene
: widget (view) coordinates → scene coordinateschart->mapFromScene
: scene coordinates → chart item coordinateschart->mapToValue
: chart item coordinates → value in a given series.The term "chart item" and "chart widget" are synonyms, since QChart
is-a QGraphicsWidget
is-a QGraphicsItem
. Note that QGraphicsWidget
is not a QWidget
!
Implementing it like this works like a charm (thanks, Marcel!):
auto const widgetPos = event->localPos();
auto const scenePos = mapToScene(QPoint(static_cast(widgetPos.x()), static_cast(widgetPos.y())));
auto const chartItemPos = chart()->mapFromScene(scenePos);
auto const valueGivenSeries = chart()->mapToValue(chartItemPos);
qDebug() << "widgetPos:" << widgetPos;
qDebug() << "scenePos:" << scenePos;
qDebug() << "chartItemPos:" << chartItemPos;
qDebug() << "valSeries:" << valueGivenSeries;