【问题标题】:Scale x-axis of QChartView using mouse wheel使用鼠标滚轮缩放 QChartView 的 x 轴
【发布时间】:2019-04-26 05:08:46
【问题描述】:

在我的应用程序中,我使用QChart 来显示折线图。不幸的是,Qt Charts 不支持使用鼠标滚轮进行缩放和鼠标滚动等基本功能。是的,有 RubberBand 功能,但仍然不支持滚动等对用户来说不太直观的端。此外,我只需要缩放 x 轴,某种setRubberBand(QChartView::HorizontalRubberBand),但使用鼠标滚轮。 到目前为止,在深入了解QChartView 之后,我使用了以下解决方法:

class ChartView : public QChartView {
protected:
    void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE
    {
        QRectF rect = chart()->plotArea();            
        if(event->angleDelta().y() > 0)
        {
            rect.setX(rect.x() + rect.width() / 4);
            rect.setWidth(rect.width() / 2);                                   
        }
        else
        {
            qreal adjustment = rect.width() / 2;
            rect.adjust(-adjustment, 0, adjustment, 0);                    
        }            
        chart()->zoomIn(rect);
        event->accept();            
        QChartView::wheelEvent(event);
    }
}

这可行,但放大然后缩小不会导致相同的结果。有一个小的偏差。调试后我发现chart()->plotArea() 总是返回相同的矩形,所以这个解决方法是没用的。

有什么方法可以只获取可见区域的矩形吗? 或者可能有人可以指出正确的解决方案如何通过鼠标为 QChartView 进行缩放/滚动?

【问题讨论】:

    标签: c++ qt qt5 qtcharts


    【解决方案1】:

    您可以使用zoom(),而不是使用zoomIn()zoomOut(),如下所示:

    class ChartView : public QChartView {
    protected:
        void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE
        {
            qreal factor = event->angleDelta().y() > 0? 0.5: 2.0;
            chart()->zoom(factor);
            event->accept();
            QChartView::wheelEvent(event);
        }
    };
    

    关于zoomIn()zoomOut(),目前还不清楚具体指的是什么坐标,我还在投资中,等有更多信息再更新答案。

    更新:

    据我观察,其中一个问题是浮点数的乘法,另一个是定位图形的中心,为了不出现这些问题,我的解决方案是重置缩放,然后设置更改:

    class ChartView : public QChartView {
        qreal mFactor=1.0;
    protected:
        void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE
        {
            chart()->zoomReset();
    
            mFactor *= event->angleDelta().y() > 0 ? 0.5 : 2;
    
            QRectF rect = chart()->plotArea();
            QPointF c = chart()->plotArea().center();
            rect.setWidth(mFactor*rect.width());
            rect.moveCenter(c);
            chart()->zoomIn(rect);
    
            QChartView::wheelEvent(event);
        }
    };
    

    【讨论】:

    • 感谢您的回答。我忘了注意,我只需要 x 轴缩放。所以缩放在这里不太有用。类似于setRubberBand(QChartView::HorizontalRubberBand),但使用鼠标滚轮。
    • @folibis 我添加了一个新的解决方案。 :D
    • 不错的解决方案,@eyllanesc!谢谢!这对我帮助很大。到目前为止,我发现的另一个解决方案只是对QChartPrivate::zoomIn() 的一个小修正,即删除高度缩放,但这需要从 QChart 创建自定义类继承。您的解决方案更优雅。
    【解决方案2】:

    我使用以下代码对 x 和 y 缩放都有效:

    void wheelEvent(QWheelEvent *event){
    qreal factor;
    if ( event->delta() > 0 )
        factor = 2.0;
    else
        factor = 0.5;
    
    QRectF r = QRectF(chart()->plotArea().left(),chart()->plotArea().top(),
                                        chart()->plotArea().width()/factor,chart()->plotArea().height()/factor);
    QPointF mousePos = mapFromGlobal(QCursor::pos());
    r.moveCenter(mousePos);
    chart()->zoomIn(r);
    QPointF delta = chart()->plotArea().center() -mousePos;
    chart()->scroll(delta.x(),-delta.y());}
    

    【讨论】:

      猜你喜欢
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多