Qt QXYSeries和ChartView - 修改在一定范围内触发的悬停行为

时间:2016-10-26 20:11:53

标签: c++ qt qtchart

我有一个由QXYSeries表示的散点图,并使用Qt Charts 5.7中的ChartView进行查看。

我想将鼠标悬停在情节上,然后徘徊"在一定距离内触发,而不是仅在我的光标直接位于某个点之上时触发。想象一下鼠标周围的圆圈,只要系列的任何部分都在其中,就会触发悬停。

有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

最后,我通过创建一个继承自QChartView并重写mouseMoveEvent(QMouseEvent* event)的类来获得此行为:

void ScatterView::mouseMoveEvent(QMouseEvent* event)
{
    if(!this->chart()->axisX() || !this->chart()->axisY())
    {
        return;
    }
    QPointF inPoint;
    QPointF chartPoint;
    inPoint.setX(event->x());
    inPoint.setY(event->y());
    chartPoint = chart()->mapToValue(inPoint);
    handleMouseMoved(chartPoint);
}

void ScatterView::handleMouseMoved(const QPointF &point)
{
    QPointF mousePoint = point;
    qreal distance(0.2); //distance from mouse to point in chart axes
    foreach (QPointF currentPoint, scatterSeries->points()) { 
        qreal currentDistance = qSqrt((currentPoint.x() - mousePoint.x())
                                      * (currentPoint.x() - mousePoint.x())
                                      + (currentPoint.y() - mousePoint.y())
                                      * (currentPoint.y() - mousePoint.y()));
        if (currentDistance < distance) {
            triggerPoint(currentPoint);
        }
    }
}