Here's how you could do it:
// create new axis rect and add it to the plot layout:
QCPAxisRect *ar = new QCPAxisRect(ui->customPlot);
ui->customPlot->plotLayout()->addElement(0, 1, ar);
// setup an extra legend for that axis rect:
QCPLegend *arLegend = new QCPLegend;
ar->insetLayout()->addElement(arLegend, Qt::AlignTop|Qt::AlignRight);
arLegend->setLayer("legend");
ui->customPlot->setAutoAddPlottableToLegend(false); // would add to the main legend (in the primary axis rect)
// create a graph in the new axis rect:
QCPGraph *graph = ui->customPlot->addGraph(ar->axis(QCPAxis::atBottom), ar->axis(QCPAxis::atLeft));
// add a legend item to the new legend, representing the graph:
// arLegend->addItem(new QCPPlottableLegendItem(arLegend, graph));
// Edit from the future: In newer QCP versions you can instead just call
graph->addToLegend(arLegend);
// this is how to remove the legend item again (e.g. before graph removal):
// arLegend->remove(arLegend->itemWithPlottable(graph));
// Edit from the future: In newer QCP versions you can instead just call
graph->removeFromLegend(arLegend);
Adding and removing from the legend is a little less convenient/automated as for the default primary legend. I've noticed there's an easier way that QCustomPlot could provide: the QCPAbstractPlottable::addToLegend
/removeFromLegend
could have an optional QCPLegend*
parameter which could make it usable also for non-default legends. I'll try to add that in version 1.3.
// Edit from the future: QCustomPlot provides this now.