QCustomPlot Discussion and Comments

Plot not showing after setting background imageReturn to overview

Hello,
I have been experiencing this intermittent problem. Most times it does not work but sometimes it does.

I initialize the QCustomPlot with a background. Then I plot some data on it. The plot does not show up.
I set up my graph as shown below:

QPen axisPen;
    axisPen.setColor(QColor("white"));

    ui->qcpLab->setBackground(QBrush(QColor("black")));


    int LabMin = -140;
    int LabMax = 140;

    QFont AxisFont = font();
    AxisFont.setPixelSize(14);
    QFont labelFont = font();
    labelFont.setPixelSize(18);
    ui->qcpLab->xAxis->setBasePen(axisPen);
    ui->qcpLab->xAxis->setRange(LabMin, LabMax-5);
    ui->qcpLab->xAxis->setLabelColor(QColor("white"));
    ui->qcpLab->xAxis->setLabelFont(labelFont);
    ui->qcpLab->xAxis->setLabel("a*");
    ui->qcpLab->xAxis->setTickLabelFont(AxisFont);
    ui->qcpLab->xAxis->setTickLabelColor(QColor("white"));
    ui->qcpLab->xAxis->setTickPen(axisPen);
    ui->qcpLab->xAxis->setVisible(false);

    ui->qcpLab->yAxis->setBasePen(axisPen);
    ui->qcpLab->yAxis->setRange(LabMin, LabMax);
    ui->qcpLab->yAxis->setLabelColor(QColor("white"));
    ui->qcpLab->yAxis->setLabelFont(labelFont);
    ui->qcpLab->yAxis->setLabel("b*");
    ui->qcpLab->yAxis->setTickLabelFont(AxisFont);
    ui->qcpLab->yAxis->setTickLabelColor(QColor("white"));
    ui->qcpLab->yAxis->setTickPen(axisPen);
    ui->qcpLab->yAxis->setVisible(false);

    QCPItemPixmap *CIEBackground = new QCPItemPixmap(ui->qcpLab);
    CIEBackground->setPixmap(QPixmap(":/images/images/CIELABChart.png"));
    CIEBackground->topLeft->setType(QCPItemPosition::ptPlotCoords);
    CIEBackground->bottomRight->setType(QCPItemPosition::ptPlotCoords);
    CIEBackground->topLeft->setCoords(ui->qcpLab->xAxis->range().lower, ui->qcpLab->yAxis->range().upper);
    CIEBackground->bottomRight->setCoords(ui->qcpLab->xAxis->range().upper, ui->qcpLab->yAxis->range().lower);
    CIEBackground->setScaled(true, Qt::IgnoreAspectRatio);

    lab_x.clear();
    lab_y.clear();
    lab_x.push_back(0.0f);
    lab_y.push_back(0.0f);
    ui->qcpLab->addGraph();
    ui->qcpLab->graph(0)->setData(lab_x, lab_y);
    ui->qcpLab->graph(0)->setPen(QPen(QBrush(Qt::white),2));
    ui->qcpLab->graph(0)->setLineStyle(QCPGraph::lsNone);
    ui->qcpLab->graph(0)->setScatterStyle(QCPScatterStyle::ssCrossCircle);

    ui->qcpLab->replot();

Sometimes when I swap the setData portion with the CIEBackground portion it works. I was working until I changed the .png file. When I tried to reverse the changes it still refused to work.

When I remove the background the plot shows up again.

Thank you.

my guess is this is a layering issue . Is there a reason you are using QCPItemPixmap for your background (normally meant images inside the plot) instead of calling QCustomPlot::setBackground?

Hello,
I found the QCPItemPixmap version of setting the background on the net and it worked for me previously.
When I went through my old code, I realized that I forgot to add

ui->qcpLab->setCurrentLayer("background");

At the beginning of the background code.
Now it seems to work.

I will try the setBackground version.

Thank you very much.

I tried the setBackground version and it is not what I wanted.
setBackground sets the background of the entire widget.
I need to set the background picture of the plot and not the whole widget.

Thanks and regards.

You can also set the background of the axis rect:
plot->axisRect()->setBackground(...)