QCustomPlot Discussion and Comments

Unexpected consequences of calling "setAutoAddPlottableToLegend(false)"Return to overview

Hi,

After calling

setAutoAddPlottableToLegend(false),

the following code (which I borrowed from an example) only generates the surrounding rectangle of the legend box but the items themselves are missing:

// legend item
ui.qcustomplot_ptr->addGraph()->setName(event_str) ;
QCPTextElement *legend_title_ptr = new QCPTextElement(ui.qcustomplot_ptr) ;
// place text element on same layer as legend, or it ends up below legend
legend_title_ptr->setLayer(ui.qcustomplot_ptr->legend->layer() ) ;
legend_title_ptr->setFont(QFont("sans", 10, QFont::Light)) ;
// if top cell isn't empty, insert an empty row at top
if (ui.qcustomplot_ptr->legend->hasElement(0, 0))
ui.qcustomplot_ptr->legend->insertRow(0) ;
// place the text element into the empty cell
ui.qcustomplot_ptr->legend->addElement(0, 0, legend_title_ptr) ;

What am I doing wrong? Originally this code added items to the automatically generated list, but now they are all gone. I must disable the automatic generation of legend items.

Thank you.

Here is what I've done so solve this:

static uint32_t legend_items ;

// legend item
ui.qcustomplot_ptr->addGraph()->setName(event_str) ;
QCPTextElement *legend_title_ptr = new QCPTextElement(ui.qcustomplot_ptr) ;
// place text element on same layer as legend, or it ends up below legend
legend_title_ptr->setLayer(ui.qcustomplot_ptr->legend->layer() ) ;
legend_title_ptr->setFont(QFont("sans", 10, QFont::Light)) ;
// if top cell isn't empty, insert an empty row at top
if (ui.qcustomplot_ptr->legend->hasElement(0, 0))
ui.qcustomplot_ptr->legend->insertRow(0) ;
// place the text element into the empty cell
ui.qcustomplot_ptr->legend->addElement(0, 0, legend_title_ptr) ;

ui.qcustomplot_ptr->graph(legend_items)->addToLegend(ui.qcustomplot_ptr->legend) ;
++legend_items ;

The only remaining issue is a large chunk of place above the items in the legend. Any ideas? Thanks.