Get mouse coordinates in the QChartView axial system

Is there a way to get the mouse coordinates in the QChartView ? Preferably in axial units. The goal is to display the coordinates of the mouse by moving the mouse around on the graph so that the user can measure the constructed objects.

I could not find a built-in function for this on QChartView , so I am trying to use QChartView::mouseMoveEvent(QMouseEvent *event) to try to calculate the resulting position in the plot area. The problem is that I cannot get a link to the coordinate system of the area region. I tried using mapToScene , mapToItem and mapToParent , as well as the reverse mapFrom... for all the objects that I can capture to try to do this, but to no avail.

I found that QChartView::chart->childItems()[2] indeed a plot area, with the exception of the axis and axis labels. Then I can call QChartView::chart->childItems()[2]->setCursor(Qt::CrossCursor) to make a cross only in the plotting area, and not on neighboring objects. But still I'm not trying to do anything to correctly refer to this coordinate system of the object.

+5
source share
1 answer

QChartView is just a QGraphicsView with built-in scene() . To get the coordinates in any of the graphs, you must go through several coordinate transformations:

  1. Let's start by viewing the coordinates of the widget
  2. view->mapToScene : widget (view) coordinates → scene coordinates
  3. chart->mapFromScene : scene coordinates → chart element coordinates
  4. chart->mapToValue : chart element coordinates → value in this series.
  5. The end of the coordinate value in this series.

QChart "chart element" and "chart widget" are synonyms because QChart is QGraphicsWidget is QGraphicsItem . Please note that QGraphicsWidget not a QWidget !

Implementing this works like a charm (thanks, Marcel!):

 auto const widgetPos = event->localPos(); auto const scenePos = mapToScene(QPoint(static_cast<int>(widgetPos.x()), static_cast<int>(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; 
+7
source

All Articles