QCustomPlot Discussion and Comments

plot->graph(0)->setSelection() gives no visual resultReturn to overview

Hi all,

I'm trying to programmatically select data ranges in a graph.

I have the following settings:

plot->setSelectionRectMode(QCP::srmSelect);
plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

When I call:

plot->graph(0)->setSelection(QCPDataSelection(QCPDataRange(idx-1,idx+1)));

there is no visual feedback. The data is still not selected in the drawing.
I see that there is a lot of code inside processRectSelection() that is not present in setSelection().

Is this expected ? How can I make the selection visually updated ?

Thanks

Found the issue. Turns out that setting the selection inside QCustomPlot::mouseRelease() is not okay since the code calling this handler does a lot of stuff after calling it.
This makes "adjusting" the selection not possible (for e.g. select a little before/after the user rect).

I had to play with flags enable/disable to make it work correctly.

void myUI::slot_plotMouseRelease(QMouseEvent *e)
{
  QCustomPlot *plt = qobject_cast<QCustomPlot *>(QObject::sender());
  if ( !plt )
    return;

  if ( e->button() != Qt::LeftButton )
    return;

  // BUG: selectionRect().isActive() is true as soon as left button is pressed
  // should be true only if mouse has moved a little
  if ( qAbs(plt->selectionRect()->rect().width()) > 3 ) {
    // unable to select data range without this flag
    plot->setInteraction(QCP::iSelectPlottables, true);
    return;
  }

  // single click with no selection -> select "single" data

  // BUG must cancel this flag so that it won't cancel
  // our selection code below because rect is null
  plot->setInteraction(QCP::iSelectPlottables, false);

  // now set custom selection
  g->setSelection(QCPDataSelection(QCPDataRange(frame-2,frame+2)));
}