Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace the deprecated function `QWheelEvent::delta()` in the zoom in / zoom out function?

Tags:

c++

qt

I was using the delta() function from the QWheelEvent class to achieve the zoom in, zoom out. now it is deprecated, and they advise in the documentation to use pixelDelta() or angleDelta() instead, but they are QPoint objects!

can anybody please tell me how to replace this deprecated function with another ones?

void MapView::wheelEvent(QWheelEvent *event)
{
    if(event->delta()>0)
    {
        if(m_scale < MAX_SCALE)
        {
            std::cout << m_scale << std::endl;
            this->scale(ZOOM_STEP, ZOOM_STEP);
            m_scale *= ZOOM_STEP;
        }
    }
    else if(event->delta() < 0)
    {
        if(m_scale >= MIN_SCALE)
        {
            std::cout << m_scale << std::endl;
            this->scale(1/ZOOM_STEP, 1/ZOOM_STEP);
            m_scale *= 1/ZOOM_STEP;
        }
    }

}
like image 413
Bilal Avatar asked Dec 07 '25 10:12

Bilal


1 Answers

The documentation of angleDelta says that angleDelta().y() will return the angle by which the vertical mouse wheel was rotated and angleDelta().x() will return the angle by which the horizontal mouse wheel was rotated.

For zooming I'm assuming you will want to use vertical scrolling, so changing the conditions accordingly gives:

void MapView::wheelEvent(QWheelEvent *event)
{
    if(event->angleDelta().y() > 0)
    {
        if(m_scale < MAX_SCALE)
        {
            std::cout << m_scale << std::endl;
            this->scale(ZOOM_STEP, ZOOM_STEP);
            m_scale *= ZOOM_STEP;
        }
    }
    else if(event->angleDelta().y() < 0)
    {
        if(m_scale >= MIN_SCALE)
        {
            std::cout << m_scale << std::endl;
            this->scale(1/ZOOM_STEP, 1/ZOOM_STEP);
            m_scale *= 1/ZOOM_STEP;
        }
    }
}
like image 108
IlCapitano Avatar answered Dec 10 '25 01:12

IlCapitano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!