Get mouse coordinates in QChartView's axis system

后端 未结 1 1019
萌比男神i
萌比男神i 2020-12-11 03:36

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

1条回答
  •  独厮守ぢ
    2020-12-11 03:44

    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:

    1. Start with the view widget coordinates
    2. view->mapToScene: widget (view) coordinates → scene coordinates
    3. chart->mapFromScene: scene coordinates → chart item coordinates
    4. chart->mapToValue: chart item coordinates → value in a given series.
    5. End with value coordinates 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;
    

    0 讨论(0)
提交回复
热议问题