Search code examples
c++qtqt5qwt

Class derived from QwtPlotZoomer not working as intended


I am writing a class MyPlotZoomer that derives from QwtPlotZoomer. MyPlotZoomer differs from QwtPlotZoomer in that in order to activate and draw the zoom rectangle, the user has to press and hold the ctrl key, click and drag, and then release mouse button.

To achieve this, I overrode three methods from QWtPlotZoomer, widgetMouseReleaseEvent, widgetMousePressEvent and widgetMouseMoveEvent in the following way:

#include <QWidget>
#include "qwt_plot.h"
#include "qwt_plot_zoomer.h"
#include <QMouseEvent>

class MyPlotZoomer : public QwtPlotZoomer
{

  public:
    // constructor
        explicit MyPlotZoomer( QWidget *canvas );
    // destructor

    // function prototypes

    // data
  protected:
    // function prototypes
    virtual void widgetMouseReleaseEvent( QMouseEvent *mouse_event ) override{
        if( mouse_event->modifiers() & Qt::ControlModifier ){
          QwtPlotZoomer::widgetMouseReleaseEvent( mouse_event );
        }
    };

    virtual void widgetMousePressEvent( QMouseEvent *mouse_event ) override{
        if( mouse_event->modifiers() & Qt::ControlModifier ){
          QwtPlotZoomer::widgetMousePressEvent( mouse_event );
        }
    };
    virtual void widgetMouseMoveEvent( QMouseEvent *mouse_event ) override{
        if( mouse_event->modifiers() & Qt::ControlModifier ){
          QwtZoomer::widgetMouseMoveEvent( mouse_event );
        }
    };


};

However, pressing ctrl key and then drawing does nothing. The zoom rectangle doesn't show up. What am I doing wrong here?


Solution

  • One of the base classes of QwtPlotZoomer is QwtEventPattern, that allows to change what type of key/mouse combinations are used. So there is no reason for deriving from QwtPlotZoomer as setKeyPattern/setMousePattern does it in a much simpler way.

    The Mouse/Key patterns being used depend on the state machine ( QwtPickerMachine ) being assigned to your zoomer. Check the corresponding class documentation.