Question

My problem is very interesting to me. I am working on Qwt and I would like to enable zoom respect X and Y axis separately. I achived zoom only X axis but Y axis didn't work. I couldn't get it. I will be glad if you give an advise.

Here is my code:

void Kmh::keyPressEvent(QKeyEvent *event)
{
    zoom_in_out = new QwtPlotMagnifier( canvas() );

    if(event->key() == Qt::Key_Shift)
    {
        zoom_in_out->setWheelModifiers(Qt::ShiftModifier);
        zoom_in_out->setAxisEnabled(Qt::XAxis,false);
    }
    else if(event->key() == Qt::Key_Control)
    {
        zoom_in_out->setWheelModifiers(Qt::ControlModifier);
        zoom_in_out->setAxisEnabled(Qt::YAxis,false);
    }
}  

shift + mousewheel is working for zoom respect X axis. But ctrl + mousewheel is zooming both X and Y axis. What am I doing wrong?

Regards

Note : Sorry for my poor English.

Was it helpful?

Solution

You completely misunderstood Qwt API. I would say you have memory leak which will remain undetectable for most of tools. QwtPlotMagnifier should be created once during construction and live as long it is needed.

I check the code and as I suspected QwtPlotMagnifier uses event filter to process events for the plot. Calling this once in construction time should do the trick:

void Kmh::setupWheelZooming()
{
    QwtPlotMagnifier *zoom_x = new QwtPlotMagnifier( canvas() );
    QwtPlotMagnifier *zoom_y = new QwtPlotMagnifier( canvas() );
    zoom_x->setWheelModifiers(Qt::ShiftModifier);
    zoom_x->setAxisEnabled(Qt::XAxis, true);
    zoom_x->setAxisEnabled(Qt::YAxis,false);
    zoom_y->setWheelModifiers(Qt::ControlModifier);
    zoom_y->setAxisEnabled(Qt::XAxis,false);
    zoom_y->setAxisEnabled(Qt::YAxis,true);
} 

This should do the trick without manual handling of any events.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top