QCustomPlot Discussion and Comments

How to add a new legend to the customPlotReturn to overview

I am calling customPlot->plotLayout()->clear(); and if I call customPlot->legend->setVisible(true);
the program crashes. Perhaps clear() clears the legend too. How can I add a new legend.

I tried customPlot->legend = new QCPLegend();

but it gives error

bool __thiscall QCPLayerable::setLayer(const class QString &) no parent QCustomPlot set
void __thiscall QCPLayerable::initializeParentPlot(class QCustomPlot *) called with parentPlot zero
bool __thiscall QCPLayerable::setLayer(const class QString &) no parent QCustomPlot set
void __thiscall QCPLayerable::initializeParentPlot(class QCustomPlot *) called with parentPlot zero

Yes of course, the legend is part of the layout and is thus deleted if you call clear(). See the corresponding documentation entry. If you then try to call something on it, your program crashes (the customPlot->legend pointer is null at that time).

For how to add legends, see here:
http://www.qcustomplot.com/index.php/support/forum/148

Thanks for the reply, but i couldn't find an answer in the reply. Can you please mention the code to add the legend.This didn't work for me:

customPlot->legend = new QCPLegend();