QCustomPlot Discussion and Comments

QCPGraph setSelected() function in new v2.0Return to overview

I have been asked to update an old piece of software that used to work correctly. It was previously compiled with QT4 and an older version of QCustomPlot (not sure off hand what version), but now I am using QT5 and QCustomPlot v2.0.0 (hash: 36e8702). I am having a problem compiling the code that should otherwise be working now. When compiling i receive this error:

src/plotWidget.cpp|104|error: ‘class QCPGraph’ has no member named ‘setSelected’|

I have a series of QCPGraph's that are being plotted on the same axis. There is also a legend that can lists all graphs. I had previously written a function that would be connected to the selectionChangedByUSer() signal. The idea was that if you select a graph in the plot window you would also select the legend item, and if you select a legend item it would select the graph.

In the current documentation there is:

Q_SLOT void 	setSelection (QCPDataSelection selection)

but it is not clear how to use this function.
Bottom line question: how can I adapt my old code to work with the new QCustomPlot framework?

Thanks for the help!

    connect(plot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));

void plotWidget::selectionChanged()
{

...

  // synchronize selection of graphs with selection of corresponding legend items:
  for (int i=0; i<plot->graphCount(); ++i)
  {
    QCPGraph *graph = plot->graph(i);
    QCPPlottableLegendItem *item = plot->legend->itemWithPlottable(graph);
    if (item->selected() || graph->selected())
    {
      item->setSelected(true);
      [b]graph->setSelected(true); <= THIS IS THE OFFENDING LINE[/b]
    }
  }
}

from the documentation, it looks like when setting up the graph, you call

graph->setSelectable(stWhole).

and then for line 16 you call graph->setSelection. i dont know what parameters you put into that function... maybe it doesnt matter as long as you have setSelectable to stWhole???

setSelection() takes an object of type QCPDataSelection. The documentation for this object talks about adding data ranges/data points to a selection, so this does not seem to me to be the correct way to select the graph...

well this is definitely the mechanism to use. it was just modified in 2.0 to allow for partial selections of data. if you have the graph set to only select the whole thing (stWhole), it should always select the whole graph. i would assume that if you create an object of QCPDataSelection, even if it doesnt have any values set, it should still select the whole graph.

I do not think the statement of Ian above is correct. If you use stWhole then you can set the selection on/off with setSelection(..) with respectively a filled data selection object or an empty data selection object.

I think this is the easiest update policy:

// 1. init to never select
pPlot->setSelectable(false);
// Version 2 This will translate to :
pPlot->setSelectable(QCP::SelectionType::stNone);

// 2.  Init to allow select
// V1:
pPlot->setSelectable(true);
// Version 2: 
pPlot->setSelectable(QCP::SelectionType::stWhole);

/// 3 now the interesting stuff
/// Programmatically enforce the selection ON
/// v1 was easy:
pPlot->setSelected(true);
/// V2 a bit more complex, set to a filled selection (anything will do here, but not empty)
pPlot->setSelection(QCPDataSelection(QCPDataRange());

/// 4 Programmatically enforce the selection OFF
// v1:
pPlot->setSelected(false);
// V2, set to empty selection
pPlot->setSelection(QCPDataSelection());