QCustomPlot Discussion and Comments

Custom legend's backgroundReturn to overview

I currently have the following image with extending QCPPlottableLegendItem https://imagebin.ca/v/55r2s5ZxqsLm
Obviously I would like to get rid of the intersections between the text and the grid lines.
How to make it so that the the legend hides the underlying grid lines? QCPLegend class does not seem to have
setBackground-type method.

From the doc

void QCPLegend::setBrush ( const QBrush & brush)
Sets the brush of the legend background.

Thanks for the answer. I've already done that -- I've called setBrush with the background color originall given to the plot itself. It is just that the grid line still cuts through.

This is the code, and I believe setBrush is not doing what I expect, since even with other colors I get basically a transparent background, i.e. setBrush is not effective:

 QCPLegend *legend= new QCPLegend;
    legend->setParent(customPlot);
    legend->setBrush(QBrush(EconLighter));
    customPlot->axisRect(0)->insetLayout()->addElement(legend,Qt::AlignTop|Qt::AlignRight);
    for ( auto i= 0; i < labels.size(); ++i ) {
        PlainLegendItem *item= new PlainLegendItem(legend,bars[i]);
        item->setParent(customPlot);
        std::string acc= labels[i].toStdString();
        acc+= " ", acc+= ticklabels[i].toStdString();
        //item->setLayer(tr(acc.c_str()));
        legend->addItem(item);
    }
    customPlot->addGraph();

Did you check the layers ? change the legend layer ?

Ok so just add :


legend->setLayer(QLatin1String("legend"));

Thank you, BSO: it worked.
https://imagebin.ca/v/55rFSK1D9Zjr

The code:

QCPLegend *legend= new QCPLegend;
    legend->setParent(customPlot);
    legend->setBrush(QBrush(EconLighter));
    customPlot->axisRect(0)->insetLayout()->addElement(legend,Qt::AlignTop|Qt::AlignRight);
    legend->setLayer(QLatin1String("legend"));
    legend->setBorderPen(Qt::NoPen);
    for ( auto i= 0; i < labels.size(); ++i ) {
        PlainLegendItem *item= new PlainLegendItem(legend,bars[i]);
        item->setParent(customPlot);
        //std::string acc= labels[i].toStdString();
        //acc+= " ", acc+= ticklabels[i].toStdString();
        //item->setLayer(tr(acc.c_str()));
        legend->addItem(item);
    }