Search code examples
c++qtqtcharts

qt convert mouse position to chart coordinate system


I would like to display a callout on a QChart with a QBarSeries displayed. I'm struggling in how to convert the position of my mouse to the qchart coordinate frame, so that I can use it as Anchor in my callout. I would like to set it during my

MainWindow::barSeriesHovered(bool status, int index, QBarSet *barset){}

event.


Solution

  • I managed to do it using following code:

    auto point = QCursor::pos();
    point = myChartView->mapFromGlobal(point);
    auto pointF = myChartView->mapToScene(point);
    pointF = myChartView->chart()->mapFromScene(point);
    pointF = myChartView->chart()->mapToValue(point,myChartView->chart()->series().at(0));
    myCallout->setAnchor(pointF);
    

    maybe not the most efficient use, but yes, it works Another drawback is, that it always appears at the border of a bar, and does not move with your mouse cursor while you move around inside the bar, but yes, it works