QCustomPlot Discussion and Comments

Override mousePressEvent of QCPAxisRectReturn to overview

I have a graph on which I want to enable both left-click range dragging and rubberband selection.

This does not seem possible by default, so I tried the following approach:
Only drag the range if the axis (here I only allow X range dragging) is selected. The following modification to the mousePressEvent method of QCPAxisRect seems to work to achieve this:

  if ((event->buttons() & Qt::LeftButton) && mParentPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis))

However, I don't want to leave the QCustomPlot code untouched and instead use a class derived from QCPAxisRect and let my QCustomPlot instance use it. I couldn't find a method like setAxisRect though.

How can I achieve this override?

Of course, it should read "I want to leave the QCustomPlot code untouched."

Have a look at the Data Selection Documentation Page, especially the section "Selection by click and selection rect", there, in the third paragraph it is explained how to dynamically switch between range dragging and selection rect. (by using signals and switching the selection rect mode there). The documentation of setSelectionRectMode also contains some info.

If you do want to reimplement QCPAxisRect, you can add an instance of your new axis rect class just like any other layout element, see the layout system documentation page.