QChart horizontal only zoom?



  • I am working on a digital waveform display where only the horizontal zooming makes sense. Is there any way to restrict the zoom to the horizontal scale. I have seen the
    zoomIn(const QRectF &) member, but I would like to avoid the continous rectangle calculation.



  • For the posterity:

    
    void ZoomableChartView::wheelEvent(QWheelEvent *event)
    {
        if (event->angleDelta().y() > 0) {
            zoomX(2, event->pos().x() - chart()->plotArea().x());
            qWarning() << chart()->plotArea();
        } else if (event->angleDelta().y() < 0) {
            zoomX(0.9, event->pos().x() - chart()->plotArea().x());
        }
    }
    
    void ZoomableChartView::zoomX(qreal factor, qreal xcenter)
    {
        QRectF rect = chart()->plotArea();
        qreal widthOriginal = rect.width();
        rect.setWidth(widthOriginal / factor);
        qreal centerScale = (xcenter / widthOriginal);
    
        qreal leftOffset = (xcenter - (rect.width() * centerScale) );
    
        rect.moveLeft(rect.x() + leftOffset);
        chart()->zoomIn(rect);
    }
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.