QCustomPlot Discussion and Comments

Rubberband selection and dragging at the same timeReturn to overview

Hello, in my QCPAxisRect I want to be able to normally drag and in addition to have a rubber-band selection. Because both is done with the left mouse button, when I activate the "setSelectionRectMode" the normal dragging isn't possible any more.


axisRect->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);



I had a look in the "QCPAxisRect::mousePressEvent". The "mDragging" variable is only true if the Qt::LeftButton is pressed:

void QCPAxisRect::mousePressEvent(QMouseEvent *event, const QVariant &details)
  if (event->buttons() & Qt::LeftButton)
    mDragging = true;

I managed the situation and subclassed the QCPAxisRect and had to override the "mousePressEvent" and used the Qt::RightButton. I wonder if that is the only possiblity? If yes, maybe in a future version it could be handy to allow the programmer to set the button specifically.

By the way thanks for this great library. I have a lot of fun using it.

Ok, to really use a rubberband with the left mouse and dragging with the right mouse I had to do more. In QCustomPlot::mousePressEvent there is a condition if the mSelectionRectMode is specified

void QCustomPlot::mousePressEvent(QMouseEvent *event)
  emit mousePress(event);
  // save some state to tell in releaseEvent whether it was a click:
  mMouseHasMoved = false;
  mMousePressPos = event->pos();
  if (mSelectionRect && mSelectionRectMode != QCP::srmNone)

My Solution was to to cancel the mSelectionRect in the mouseMoveEvent of my subclassed plot, only when the right Button was pressed:

void QMyPlot::mouseMoveEvent(QMouseEvent *event)
	if(myPressedButton == Qt::RightButton)
		if (mSelectionRect && mSelectionRect->isActive())


Maybe this behaviour can be achieved with subclassing QCPSelectionRect. But for the moment I'm fine with that solution.

Ok ok, it's good to read the documentation. My second post can be deleted.


If you wish to provide your user both with axis range dragging and data selection/range zooming, use this method to switch between the modes just before the interaction is processed, e.g. in reaction to the mousePress or mouseMove signals.