Hi all, could somebody elaborate on pu-lnull-mezon's comment?
Below is what I'm currently using, but it is not reliable as: if you select a graph and then click on a point, it will show the correct value. However, if you select a point the second time (while the graph is still selected), it will show the value from the first click after graph selection. Only upon clicking on the new point a second time will it show the correct value.
bool MainWindow::eventFilter(QObject *target, QEvent *event)
{
}if(target == ui->myPlot && event->type() == QEvent::MouseButtonPress &&
ui->myPlot->selectedGraphs().size() > 0)
{
QMouseEvent *_mouseEvent = static_cast<QMouseEvent*>(event);
// find a data point's value at closest coordinate to mouse click
double xCoord = ui->myPlot->xAxis->pixelToCoord(_mouseEvent->pos().x());
double yCoord = ui->myPlot->yAxis->pixelToCoord(_mouseEvent->pos().y());
// works but has an issue:
// if the first click after graph selection was on the graph, the second click will be incorrect
// must click on the plot point twice
// this is absolute magic to me... http://www.qmyPlot.com/documentation/dataselection.html#dataselection-accessing
QCPDataSelection selection = ui->myPlot->selectedGraphs().front()->selection();
foreach (QCPDataRange dataRange, selection.dataRanges())
{
QCPGraphDataContainer::const_iterator begin = ui->myPlot->selectedGraphs().front()->data()->at(dataRange.begin()); // get range begin iterator from index
QCPGraphDataContainer::const_iterator end = ui->myPlot->selectedGraphs().front()->data()->at(dataRange.end()); // get range end iterator from index
int i = 0;
for (QCPGraphDataContainer::const_iterator it=begin; it!=end; ++it)
{
// iterator "it" will go through all selected data points
qDebug("Value closest to (%f,%f) on graph: %f", xCoord, yCoord, it->value);
i++;
}
}
}
return false;
Cheers