Search code examples
qt5qcustomplot

QCustomPlot: mouse interaction on secondary axis


I have a QCustomPlot with all the 4 axes enabled and with these interactions activated:

my_w.plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | QCP::iSelectLegend | QCP::iSelectPlottables);

Now by swiping on an xAxis or yAxis I can change the scale, but when I do the same over xAxis2 or yAxis2 nothing happens.

how do I set interaction over the secondary axes?

EDIT:

I've discovered setRangeDragAxes and setRangeZoomAxes:

my_w.plot->axisRect()->setRangeDragAxes(my_w.plot->xAxis2,my_w.plot->yAxis2);
my_w.plot->axisRect()->setRangeZoomAxes(my_w.plot->xAxis2,my_w.plot->yAxis2);

now I can drag/and zoom axes, and everything it's almost ok: drag works ok, but When I zoom by swiping with two fingers, both xAxis2 and yAxis2 zoom together.


Solution

  • You can make only one axis zoom by selecting the axis you want to zoom and add a mouseWheel slot.

    Connecting mouse wheel signal to your slot:

    connect(my_w.plot, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel()));
    

    Implement mouse wheel slot:

    void YourDialog::mouseWheel()
    {
      // if an axis is selected, only allow the direction of that axis to be zoomed
      // if no axis is selected, both directions may be zoomed
    
      if (my_w.plot->xAxis->selectedParts().testFlag(QCPAxis::spAxis)){
        my_w.plot->axisRect()->setRangeZoomAxes(my_w.plot->xAxis,my_w.plot->yAxis);
        my_w.plot->axisRect()->setRangeZoom(my_w.plot->xAxis->orientation());
      }
      else if (my_w.plot->yAxis->selectedParts().testFlag(QCPAxis::spAxis)){
        my_w.plot->axisRect()->setRangeZoomAxes(my_w.plot->xAxis,my_w.plot->yAxis);
        my_w.plot->axisRect()->setRangeZoom(my_w.plot->yAxis->orientation());
      }
      else if (my_w.plot->xAxis2->selectedParts().testFlag(QCPAxis::spAxis)){
        my_w.plot->axisRect()->setRangeZoomAxes(my_w.plot->xAxis2,my_w.plot->yAxis2);
        my_w.plot->axisRect()->setRangeZoom(my_w.plot->xAxis2->orientation());
      }
      else if (my_w.plot->yAxis2->selectedParts().testFlag(QCPAxis::spAxis)){
        my_w.plot->axisRect()->setRangeZoomAxes(my_w.plot->xAxis2,my_w.plot->yAxis2);
        my_w.plot->axisRect()->setRangeZoom(my_w.plot->yAxis2->orientation());
      }
      else
        my_w.plot->axisRect()->setRangeZoom(Qt::Horizontal|Qt::Vertical);
    }
    

    You may change the last case to setRangeZoom(0) if you don't want to make any zoom when none of the axes are selected.

    Take a look at the Interaction Example for more options.