QCustomPlot Discussion and Comments

QCP 2.0 Legend setSelected()Return to overview

Hi there,

after upgrade from v1.3.2 to v2.0.0_beta, I have several problems with QCPLegend...
1. legendClick() signal wouldn't be emitted from QCustomPlot class
2. It seems QCPLegend::setSelected() is not working

I am not sure where to problem is, can you help? Any ideas?

Any Ideas?

What is the preferred way in QCP2.0 to select from legend? On selection, the graph shall be displayed as selected... On selection from plotter, legend item shall be displayed selected...

Having a similar issue here...

Does anybody additional have this issue?

I can confirm QCustomPlot::legendClick() worked in 1.3 and doesn't work in 2.0.

http://www.qcustomplot.com/index.php/tutorials/userinteractions still mentions this signal (as do the API docs), so I think this wasn't deprecated.

Manu, can you please advise?



the click signals are buggy in the beta, this is already fixed for the final.


I cannot find code more recent than the buggy version, neither a 2.0 roadmap. Could you share either or both, please?



The legendClick signal also didn't work for me, but I noticed that the legendDoubleClick works. So I just copied some of the code from QCustomPlot::mouseDoubleClickEvent to QCustomPlot::mouseReleaseEvent and - after some adjustments - it now works. Here is the resulting code for anybody who cannot wait for the next release - its at own risk, its a quick and possibly dirty solution:

void QCustomPlot::mouseReleaseEvent(QMouseEvent *event)
    emit mouseRelease(event);

    // determine layerable under the cursor (this event is called instead of the second press event in a double-click):
    QList<QVariant> details;
    QList<QCPLayerable*> candidates = layerableListAt(mMousePressPos, false, &details);
    for (int i=0; i<candidates.size(); ++i)
      event->accept(); // default impl of QCPLayerable's mouse events ignore the event, in that case propagate to next candidate in list
      candidates.at(i)->mouseDoubleClickEvent(event, details.at(i));
      if (event->isAccepted())
        mMouseEventLayerable = candidates.at(i);
        mMouseEventLayerableDetails = details.at(i);

    if (!mMouseHasMoved) // mouse hasn't moved (much) between press and release, so handle as click
      if (mSelectionRect && mSelectionRect->isActive()) // a simple click shouldn't successfully finish a selection rect, so cancel it here
      if (event->button() == Qt::LeftButton)

      // emit specialized click signals of QCustomPlot instance:
      if (!candidates.isEmpty())
        if (QCPAbstractPlottable *ap = qobject_cast<QCPAbstractPlottable*>(candidates.first()))
          int dataIndex = 0;
          if (!details.first().value<QCPDataSelection>().isEmpty())
            dataIndex = details.first().value<QCPDataSelection>().dataRange().begin();
          emit plottableClick(ap, dataIndex, event);
        } else if (QCPAxis *ax = qobject_cast<QCPAxis*>(candidates.first()))
          emit axisClick(ax, details.first().value<QCPAxis::SelectablePart>(), event);
        else if (QCPAbstractItem *ai = qobject_cast<QCPAbstractItem*>(candidates.first()))
          emit itemClick(ai, event);
        else if (QCPLegend *lg = qobject_cast<QCPLegend*>(candidates.first()))
          emit legendClick(lg, 0, event);
        else if (QCPAbstractLegendItem *li = qobject_cast<QCPAbstractLegendItem*>(candidates.first()))
          emit legendClick(li->parentLegend(), li, event);

    if (mSelectionRect && mSelectionRect->isActive()) // Note: if a click was detected above, the selection rect is canceled there
      // finish selection rect, the appropriate action will be taken via signal-slot connection:
    } else
      // call event of affected layerable:
      if (mMouseEventLayerable)
        mMouseEventLayerable->mouseReleaseEvent(event, mMousePressPos);
        mMouseEventLayerable = 0;

    if (noAntialiasingOnDrag())

    event->accept(); // in case QCPLayerable reimplementation manipulates event accepted state. In QWidget event system, QCustomPlot wants to accept the event.


I also had a problem with :
QCPGraph::setSelection(QCPDataSelection selection).

After some tests, I realised that when creating the QCPDataRange must be set from the desired first index to the desired last index +1. For instance, I created a selection like this :

    QCPDataSelection test(QCPDataRange(1,2));

and the output using:
    qDebug() << test;

is : QCPDataSelection([1..1] )

I don't know if that is the desired behavior, but it is a bit weird imo.

Ha !, and use QCustomPlot::replot()...
else you will not see the selection change.