2

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.

Abhishek
  • 6,912
  • 14
  • 59
  • 85
alperyazir
  • 203
  • 1
  • 4
  • 14
  • I don't know much about Qwt, But is there any default behaviour implemented for Ctrl+mousewheel events? – epsilon Jan 02 '14 at 09:04
  • Why are you using `keyPressEvent`? You can just use `wheelEvent` and check `QApplication::keyboardModifiers()` value to check if Shift or Ctrl is pressed. – Pavel Strakhov Jan 02 '14 at 10:12

1 Answers1

5

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.

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Thanks for reply @Marek. I tried your code but result is same. X axis is OK but It doesn't work with respect to Y axis. here is the video that you can understand clearly.[link](http://www.youtube.com/watch?v=qfyKBv22H3U) – alperyazir Jan 02 '14 at 11:45
  • so maybe problem is default values? try code update (2 extra lines). – Marek R Jan 02 '14 at 11:57
  • [that is not a problem](http://docs.mitk.org/0.12/qwt__plot__magnifier_8cpp-source.html) all axis by default are enabled. I would try different `WheelModifiers`, maybe there is some problem. – Marek R Jan 02 '14 at 12:03
  • 1
    I figure out now. It should be **QwtPlot::xBottom** and **QwtPlot::yLeft**. It's working properly now. Thank you for your interest. – alperyazir Jan 02 '14 at 12:26
  • :), good lesson read documentation carefully. Anyway API should use type `QwtPlot::Axis` instead of `int` then this would be easily avoided. I hope that at least I've helped you properly use `QwtPlotMagnifier`. – Marek R Jan 02 '14 at 12:50